This is an old revision of the document!


Using XML

Editing XML

Normally, binding a ListView to a datasource (the xml) and let WPF databinding handle the update of the XML data automatically. To do this you could create a DataSet from your XML and then bind that using ListView.ItemsSource. You would then create a DataTemplate to define the visual representation of a record in your xml. This could be input controls that'd allow you do directly edit the record within your listview. If you prefer a master-detail style view you would bing the detail view to the current item of your listview (e.g. UserControl.DataContext={Binding CurrentItem, ElementName=myListView}). The rest will be handled by WPF. UPDATE: Here is an example how you could even bind direcly to your XDocument, so so you do not necessarily have to use DataSets. (Source: bitbonk on StackOverflow.com)

Parsing XML

Example (Source: Lukas Šalkauskas:

XmlDocument xmlDoc= new XmlDocument(); //* create an xml document object.
xmlDoc.Load("yourXMLFile.xml"); //* load the XML document from the specified file.
 
//* Get elements.
XmlNodeList girlAddress = xmlDoc.GetElementsByTagName("gAddress");
XmlNodeList girlAge = xmlDoc.GetElementsByTagName("gAge"); 
XmlNodeList girlCellPhoneNumber = xmlDoc.GetElementsByTagName("gPhone");
 
//* Display the results.
Console.WriteLine("Address: " + girlAddress[0].InnerText);
Console.WriteLine("Age: " + girlAge[0].InnerText);
Console.WriteLine("Phone Number: " + girlCellPhoneNumber[0].InnerText);

Technologies and libraries to manipulate XML in .NET:

XmlDocument: If you are after one specific element, you can access child elements with the indexer: xmlDoc[“Root”], and these can be chained: xmlDoc[“Root”][“Folder”][“Item”] to dig down the hierarchy (although it's sensible to validate that these elements actually exist) – (Source: Jason Williams)

If you're processing a large amount of data (many megabytes) then you want to be using XmlReader to stream parse the XML. Anything else (XPathNavigator, XElement, XmlDocument and even XmlSerializer if you keep the full generated object graph) will result in high memory usage and also a very slow load time. Of course, if you need all the data in memory anyway, then you may not have much choice. (Source: Simon Steele)

References