Using the XML component to parse and write XML


The IPWorks 2016 XML component replaces the XMLp and XMLw components from previous versions. The XML component combines the functionality of the old components, making it easier to write code which requires both XML parsing and writing functionality.

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# // Continued from the code snipped above xml1.XPath = "/weatherdata"; // Add a new 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.