Inserting XmlAttributes into an XmlDocument


Inserting XmlAttributes into an XmlDocument

XmlAttributes can be inserted into a document by using the SetAttribute method described earlier in this chapter. Alternatively, you can add them using the insertion methods on the XmlAttributeCollection class. The insertion methods, similar to the XmlNode insertion methods, are Append , Prepend , InsertBefore , and InsertAfter .

Append and Prepend add an XmlAttribute to the end of the attribute list and to the beginning of the attribute list, respectively. The methods return the XmlAttribute that was inserted. If there exists an attribute in the collection with the same name , then the original attribute is removed from the collection, and the new attribute is added. Listing 11.23 illustrates how to use Append and Prepend .

Listing 11.23
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root/>"); XmlAttribute first = doc.CreateAttribute("att1"); first.Value = "first"; XmlAttribute second= doc.CreateAttribute("att2"); second.Value = "second"; XmlAttributeCollection atts = doc.DocumentElement.Attributes; atts.Prepend(first); atts.Append(second); doc.Save("blah.xml"); VB Dim doc As New XmlDocument() doc.LoadXml("<root/>"); Dim first As doc.CreateAttribute("att1") first.Value = "first" Dim second= doc.CreateAttribute("att2") second.Value = "second" Dim atts As doc.DocumentElement.Attributes atts.Prepend(first) atts.Append(second) doc.Save("blah.xml") 

InsertBefore and InsertAfter add an XmlAttribute before and after a specified reference attribute, respectively. If the reference node is null, then the InsertBefore method will insert the attribute at the end of the collection, whereas InsertAfter inserts the attribute at the beginning of the collection. If the reference attribute is not in the collection, then an ArgumentException is raised. As with Append and Prepend , when an existing attribute has the same name, it will be removed, and the new attribute will be inserted. Listing 11.24 shows how to use these two methods.

Listing 11.24
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"first\" att4=\"last\"/>"); XmlAttribute second = doc.CreateAttribute("att2"); second.Value = "2nd"; XmlAttribute third = doc.CreateAttribute("att3"); third.Value = "3rd"; XmlAttributeCollection atts = doc.DocumentElement.Attributes; XmlAttribute first = atts[0]; XmlAttribute last = atts[1]; atts.InsertAfter(second, first); atts.InsertBefore(third, last); doc.Save("blah.xml"); VB Dim doc As New XmlDocument() doc.LoadXml("<root att1="&Chr(34)&"first"&Chr(34)& _                    "att4="&Chr(34)&"last"&Chr(34)& "/>") Dim second As doc.CreateAttribute("att2") second.Value = "2nd" Dim third A doc.CreateAttribute("att3") third.Value = "3rd"; Dim atts As doc.DocumentElement.Attributes Dim first As atts[0] Dim last As atts[1] atts.InsertAfter(second, first) atts.InsertBefore(third, last) doc.Save("blah.xml") 


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