New to Telerik UI for WPFStart a free 30-day trial

Using XML

Updated on Sep 24, 2025

The purpose of this tutorial is to show you how to read data from XML document and use it in the context of your WPF application. The following common tasks will be examined:

  • Deserializing data.

  • Reading data with Linq to XML.

  • Reading data with XMLReader.

Deserializing Data

The most straight-forward way to read data from XML document is to deserialize it using the XmlSerializer class. Here is a sample code showing how this can be achieved:

C#
	private XmlNodeItemList RetrieveData()
	{
	    string xmlDocument = "/DataSource/XmlData.xml";
	    // 1. Get the resource stream for the file located in the application package.
	    Stream xmlStream = Application.GetResourceStream( new Uri( xmlDocument, UriKind.Relative ) ).Stream;
	    // 1. Get the stream for the file located on the local hard drive.
	    Stream xmlStream = File.Open( xmlDocument, FileMode.Open );
	    // 2. Create a new instance of the XmlSerializer class.
	    XmlSerializer serializer = new XmlSerializer( typeof( XmlNodeItemList ) );
	    // 3. Create a new instance of the StreamReader class.
	    StreamReader reader = new StreamReader( xmlStream );
	    // 4. Deserialize the data
	    XmlNodeItemList list = (XmlNodeItemList)serializer.Deserialize( reader );
	}

Reading Data With Linq to XML

Before reading you should load your XML file to an XElement or XDocument object. This can be done with the Load() method. You can input from string, from TextReader, from XMLReader and of course from file:

C#
	XDocument myXML = XDocument.Load( "MyXML.xml" );

On the next step you can use standard query operators to read the XML. Here is a sample code:

C#
	List<Person> personsList =   
    ( from person in myXml.Descendants( "person" )   
      where (( string )person.Element( "address" ).Attribute( "country" )).Equals( "Bulgaria" )
      select new Person()
      {   
          FirstName = person.Element( "firstName" ).Value,
          LastName = person.Element( "lastName" ).Value,
          Address = new Location()
          {  
              City = person.Element( "address" ).Attribute( "city" ).Value,
              Country = person.Element( "address" ).Attribute( "country" ).Value
          }  
      } ).ToList();

Reading Data with XMLReader

The following example navigates through a stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.

C#
	using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
	{
	    XmlWriterSettings ws = new XmlWriterSettings();
	    ws.Indent = true;
	    using (XmlWriter writer = XmlWriter.Create(output, ws))
	    {
	        // Parse the file and display each of the nodes.
	        while (reader.Read())
	        {
	            switch (reader.NodeType)
	            {
	                case XmlNodeType.Element:
	                    writer.WriteStartElement(reader.Name);
	                    break;
	                case XmlNodeType.Text:
	                    writer.WriteString(reader.Value);
	                    break;
	                case XmlNodeType.XmlDeclaration:
	                case XmlNodeType.ProcessingInstruction:
	                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
	                    break;
	                case XmlNodeType.Comment:
	                    writer.WriteComment(reader.Value);
	                    break;
	                case XmlNodeType.EndElement:
	                    writer.WriteFullEndElement();
	                    break;
	            }
	        }
	    }
	}

See Also