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:
CopyC#
private XmlNodeItemList RetrieveData()
{
string xmlDocument = "/DataSource/XmlData.xml";
Stream xmlStream = Application.GetResourceStream( new Uri( xmlDocument, UriKind.Relative ) ).Stream;
Stream xmlStream = File.Open( xmlDocument, FileMode.Open );
XmlSerializer serializer = new XmlSerializer( typeof( XmlNodeItemList ) );
StreamReader reader = new StreamReader( xmlStream );
XmlNodeItemList list = (XmlNodeItemList)serializer.Deserialize( reader );
}
CopyVB.NET
Private Function RetrieveData() As XmlNodeItemList
Dim xmlDocument As String = "/DataSource/XmlData.xml"
Dim xmlStream As Stream = Application.GetResourceStream(New Uri(xmlDocument, UriKind.Relative)).Stream
Dim xmlStream As Stream = File.Open(xmlDocument, FileMode.Open)
Dim serializer As New XmlSerializer(GetType(XmlNodeItemList))
Dim reader As New StreamReader(xmlStream)
Dim list As XmlNodeItemList = DirectCast(serializer.Deserialize(reader), XmlNodeItemList)
End Function
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:
CopyC#
XDocument myXML = XDocument.Load( "MyXML.xml" );
CopyVB.NET
Dim myXML As XDocument = XDocument.Load("MyXML.xml")On the next step you can use standard query operators to read the XML. Here is a sample code:
CopyC#
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();
CopyVB.NET
Dim personsList As List(Of Person) = (From person In myXml.Descendants("person") _
Where DirectCast(person.Element("address").Attribute("country"), String).Equals("Bulgaria") _
Select New Person()).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.
CopyC#
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
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;
}
}
}
}
CopyVB.NET
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
Dim ws As New XmlWriterSettings()
ws.Indent = True
Using writer As XmlWriter = XmlWriter.Create(output, ws)
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(reader.Name)
Exit Select
Case XmlNodeType.Text
writer.WriteString(reader.Value)
Exit Select
Case XmlNodeType.XmlDeclaration, XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(reader.Name, reader.Value)
Exit Select
Case XmlNodeType.Comment
writer.WriteComment(reader.Value)
Exit Select
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
Exit Select
End Select
End While
End Using
End Using
See Also