Accessing Markup from Nodes


There may be times when you need to access an element's contents in markup form. The XmlNode class provides two properties for retrieving a node's XML markup content: OuterXml and InnerXml . OuterXml returns the content of the current node, with the current node as the root element. The InnerXml property returns only the content of the current element. The XML returned from InnerXml should be considered an XML fragment. Listing 11.10 shows how to use OuterXml and InnerXml to display the markup content of the node.

Listing 11.10
 C# XmlDocument doc = new XmlDocument(); Doc.LoadXml("<Book>" +             "<Title>Catcher in the Rye</Title>" +             "<Price>25.98</Price>" +             "</Book>"); MessageBox.Show("OuterXml:" + doc.DocumentElement.OuterXml); MessageBox.Show("InnerXml:" + doc.DocmentElement.InnerXml); VB Dim doc As New XmlDocument() Doc.LoadXml("<Book>" & _             "<Title>Catcher in the Rye</Title>" & _             "<Price>25.98</Price>" & _             "</Book>") MessageBox.Show("OuterXml:" & doc.DocumentElement.OuterXml) MessageBox.Show("InnerXml:" & doc.DocmentElement.InnerXml) 

These two methods are useful when a node's XML content should be considered separately from the document. For example, consider several classes that can use XML to initialize their data. All the XML data is being stored in a single XML file. There is an object that walks through the data and hands XML fragments to the various objects for initialization.

The two classes in Listing 11.11 exemplify how this may be designed. The AppSerializer class contains a method named ReadStockQuote , which reads an XML document, finds the stock quote node, and hands the outer XML of the node to the StockQuote object for initialization. The StockQuote class has a method called ReadXml that initializes its data from the XML string.

Listing 11.11
 C# class AppSerializer {   StockQuote ReadStockQuote(string filename)   {     XmlDocument doc = new XmlDocument();     StreamReader xmlFile = new StreamReader(filename);     doc.Load(xmlFile);     xmlFile.Close();     // find the stock quote object     XmlNode quoteNode = doc.DocumentElement;     // hand outer xml to the quote object     StockQuote quoteObj = new StockQuote();     quoteObj.ReadXml(quoteNode.OuterXml);     return quoteObj;   } } class StockQuote {   string Name;   string Price;   void ReadXml(string xmlString)   {     // read the xml from the string   } } VB Public Class AppSerializer   Public Sub StockQuote ReadStockQuote(filename As String)     Dim doc As New XmlDocument()     Dim xmlFile As New StreamReader(filename)     doc.Load(xmlFile)     xmlFile.Close()     ' find the stock quote object     Dim quoteNode As doc.DocumentElement;     ' hand outer xml to the quote object     Dim quoteObj As New StockQuote()     quoteObj.ReadXml(quoteNode.OuterXml)     return quoteObj;   End Sub End Class Public Class StockQuote   Public Sub ReadXml(string xmlString)    ' read the xml from the string    End Sub End Class 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net