Creating Other Node Types


Of course, the XmlDocument class provides methods for creating every node type. Table 11.2 lists the additional System.Xml.XmlNodeTypes and their corresponding creation methods.

Table 11.2. Other XmlNodeTypes and Their Creation Methods

NODETYPE

CREATION METHOD

CDATA

CreateCDataSection(string data)

Comment

CreateComment(string data)

DocumentFragment

CreateDocumentFragment()

EntityReference

CreateEntityReference(string name )

ProcessingInstruction

CreateProcessingInstruction(string target, string data)

Whitespace

CreateWhitespace(string ws)

Text

CreateTextNode(string text)

SignificantWhitespace

CreateSignificantWhitespace(string ws)

The CreateEntityReference method must be handled with care. The method will accept any legal name that can be used to create an EntityReference node. This entity reference will be written out when the document is saved. The catch is that unless the name parameter is equal to one of the general entities ( gt , lt , apos , quot , amp ), a NotSupportedException will be raised when the document is loaded back into an XmlDocument . Listing 11.20 will raise an exception at line 9 because of this.

Listing 11.20
 C# XmlDocument doc = new XmlDocument(); XmlElement ele = doc.CreateElement("", "root", ""); doc.AppendChild(ele); ele.AppendChild(doc.CreateEntityReference("entity")); doc.Save("blah.xml"); XmlDocument doc2 = new XmlDocument(); doc.Load(new StreamReader("blah.xml")); string val = doc.DocumentElement.FirstChild.Value; VB Dim doc As New XmlDocument() Dim ele As doc.CreateElement("", "root", "") doc.AppendChild(ele) ele.AppendChild(doc.CreateEntityReference("entity")) doc.Save("blah.xml") Dim doc2 As New XmlDocument(); doc.Load(New StreamReader("blah.xml")) Dim val As doc.DocumentElement.FirstChild.Value; 


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