Using the XML component to parse and write XML


Parsing XML

The XML component can be used to parse XML from various input sources. The code below parses XML from a file using the InputFile property, but it can also parse from an InputStream or a string by using the SetInputStream method or the InputData property instead.

C#

Xml xml1 = new Xml(); // Set the file path to parse. xml1.InputFile = "C:\weatherdata.xml"; xml1.Parse(); // Get temperature and humidity data. xml1.XPath = "/weatherdata/temp"; string temp = xmlp1.XText; xml1.XPath = "/weatherdata/humidity"; string humidity = xmlp1.XText;

Writing XML

The XML component can be used to write XML out to various output destinations. The code below writes XML to a file using the OutputFile property, but it can also write to an OutputStream or a string by using the SetOutputStream method or the OutputData property instead.

C#

xml1.XPath = "/weatherdata"; // Write an element "rainfall" within the "weatherdata" element, and set its value to "2.34" xml1.PutElement("rainfall", "", "2.34"); xml1.OutputFile = "C:\weatherdata.xml"; xml1.Overwrite = true; xml1.Save();

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.