by Euo
16. March 2010 14:32
Let say in your code you already have an instance of XmlDocument and you want to manipulate it easily, you can use the XDocument to do that.
Just for example, let us load an xml from existing file.
// read the xml
StreamReader srr = new StreamReader(@"c:\composite.xml");
// load in XmlDocument
XmlDocument doc = new XmlDocument();
doc.Load(srr);
// create XmlNodeReader
XmlNodeReader nodereader = new XmlNodeReader(doc);
// load in XDocument
XDocument xd = XDocument.Load(nodereader);
// you can now traverse in its descendants much more easily
foreach (var x in xd.Descendants())
{
if (x.NodeType == XmlNodeType.Element && !x.HasElements)
{
Console.WriteLine("Name:{0}, Value:{1}", x.Name, x.Value);
x.SetValue((object)x.Value + "_x");
}
}
// save it for comparison
xd.Save(@"C:\composite_xd.xml", SaveOptions.None);
// load it back to XmlDocument
doc.Load(xd.CreateReader());
// save it for comparison
doc.Save(@"C:\composite_xmldoc.xml");
39bbfe14-ab44-4662-8b44-b003e478fe56|0|.0
Tags:
Development