Identifying the Markup of a Node |
|
The Inner Text of a node |
In the previous sections, we have seen how to create a tag to produce a node. We also saw that a node could be placed inside of another node. The combined text of the values of the children of a node is available through its XmlNode.InnerText property which is declared as follows: public virtual string InnerText{get; set}; |
This property concatenates the values of the children of the node that called them and doesn't include their markups. Here is an example: using System; using System.IO; using System.Xml; namespace VideoCollection1 { class Program { static int Main(string[] args) { string strFilename = "Videos.xml"; XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(strFilename)) { xmlDoc.Load(strFilename); XmlElement elm = xmlDoc.DocumentElement; Console.WriteLine("{0}", elm.InnerText); } else Console.WriteLine("The file {0} could not be located", strFilename); Console.WriteLine(); return 0; } } } This would produce: The Distinguished GentlemanJonathan Lynn112 MinutesDVDRHer AlibiBruce Beresford9 4 MinsDVDPG-13Chalte ChalteAziz Mirza145 MinsDVDN/R Press any key to continue . . . Notice that this property produces all values of the children of a node in one block. We already saw how to access each value of the children of a node by calling the XmlTextReader.Read() method and get its Text.
If you want to get a node, its markup, its child(ren) and its(their) markup(s), you can access its XmlNode.OuterXml property which is declared as follows: public virtual string OuterXml{get}; Here is an example: using System; using System.IO; using System.Xml; namespace VideoCollection1 { class Program { static int Main(string[] args) { string strFilename = "Videos.xml"; XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(strFilename)) { xmlDoc.Load(strFilename); XmlElement elm = xmlDoc.DocumentElement; Console.WriteLine("{0}", elm.OuterXml); } else Console.WriteLine("The file {0} could not be located", strFilename); Console.WriteLine(); return 0; } } } This would produce: <Videos><Video><Title>The Distinguished Gentleman</Title><Director>Jonathan Lynn </Director><Length>112 Minutes</Length><Format>DVD</Format><Rating>R</Rating></V ideo><Video><Title>Her Alibi</Title><Director>Bruce Beresford</Director><Length> 94 Mins</Length><Format>DVD</Format><Rating>PG-13</Rating></Video><Video><Title> Chalte Chalte</Title><Director>Aziz Mirza</Director><Length>145 Mins</Length><Fo rmat>DVD</Format><Rating>N/R</Rating></Video></Videos> Press any key to continue . . .
If you want only the markup(s) of the child(ren) excluding the parent, access its XmlNode.InnerXml property which is declared as follows: public virtual string InnerXml{get}; Here is an example: Console.WriteLine("{0}", elm.InnerXml); This would produce: <Video><Title>The Distinguished Gentleman</Title><Director>Jonathan Lynn</Direct or><Length>112 Minutes</Length><Format>DVD</Format><Rating>R</Rating></Video><Vi deo><Title>Her Alibi</Title><Director>Bruce Beresford</Director><Length>94 Mins< /Length><Format>DVD</Format><Rating>PG-13</Rating></Video><Video><Title>Chalte C halte</Title><Director>Aziz Mirza</Director><Length>145 Mins</Length><Format>DVD </Format><Rating>N/R</Rating></Video> Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|