Removing XmlAttribute s from their XmlElement s


Removing XmlAttribute s from their XmlElement s

The API for removing attributes is much richer than that for removing other nodes. This is due to the ability to remove nodes through methods on the XmlAttributeCollection class as well as methods on the XmlElement class.

The XmlAttributeCollection class provides three methods for removing an XmlAttribute : Remove , RemoveAt , and RemoveAll . The Remove method removes a single specified attribute from the attribute collection. The RemoveAt method removes a single attribute corresponding to the specified index from the collection. The RemoveAll method removes all of the attributes in the collection. Listing 11.28 demonstrates how to use each method.

Listing 11.28
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root><child att1=\"val\" " +             "att2=\"val\"/><child att=\"val\" "+             "att3=\"val\"/></root>"); XmlAttributeCollection atts =   doc.DocumentElement.FirstChild.Attributes; atts.Remove(atts[0]); atts.RemoveAt(0); atts = doc.DocumentElement.LastChild.Attributes; atts.RemoveAll(); doc.Save("out.xml"); VB Dim doc As New XmlDocument() doc.LoadXml("<root><child att1="&Chr(34)&"val"&Chr(34)& _             "att2="&Chr(34)&"val"&Chr(34)& "/>" & _             "<child att="&Chr(34)&"val"&Chr(34)& _             "att3="&Chr(34)&"val"&Chr(34)&"/>" & _              "</root>"); Dim atts As doc.DocumentElement.FirstChild.Attributes atts.Remove(atts(0)) atts.RemoveAt(0); atts = doc.DocumentElement.LastChild.Attributes atts.RemoveAll() doc.Save("out.xml") 

The XmlElement also provides three methods for removing attributes: RemoveAttributeNode , RemoveAttributeAt , RemoveAllAttributes . The RemoveAttributeNode is just like XmlAttributeCollection.Remove : It removes a single specified attribute from the element's attribute collection. There is also RemoveAttribute , which takes the name of the attribute, as opposed to the XmlAttribute object, and removes the attribute with the corresponding name. The RemoveAttributeAt method corresponds to XmlAttributeCollection 's RemoveAt method: It removes a single attribute corresponding to the specified index from the element's attribute collection. Finally, the RemoveAllAttributes collection removes all attributes from an element's attribute collection, much like XmlAttributeCollection.RemoveAll . Listing 11.29 demonstrates how to use each method.

Listing 11.29
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root><child att1=\"val\" " +              "att2=\"val\" att3=\"val\"/> " +                   "<child att=\"val\" "+                   "att3=\"val\"/></root>"); XmlElement firstChild = (XmlElement)doc.DocumentElement.FirstChild; XmlAttributeCollection atts = firstChild.Attributes; firstChild.RemoveAttributeNode(atts[0]); firstChild.RemoveAttribute("att2"); firstChild.RemoveAttributeAt(0); XmlElement lastChild = (XmlElement)doc.DocumentElement.LastChild; atts = lastChild.Attributes; lastChild.RemoveAllAttributes(); doc.Save("out.xml"); VB Dim doc As New XmlDocument() doc.LoadXml("<root><child att1="&Chr(34)&"val"&Chr(34)& _             "att2="&Chr(34)&"val"&Chr(34)& _             "att3="&Chr(34)&"val"&Chr(34)&"/>" & _              "<child att="&Chr(34)&"val"&Chr(34)& _             "att3="&Chr(34)&"val"&Chr(34)&"/></root>") Dim firstChild As XmlElement firstChild = doc.DocumentElement.FirstChild Dim atts As firstChild.Attributes firstChild.RemoveAttributeNode(atts(0)) firstChild.RemoveAttribute("att2") firstChild.RemoveAttributeAt(0) Dim lastChild As XmlElement lastChild = doc.DocumentElement.LastChild atts = lastChild.Attributes lastChild.RemoveAllAttributes() doc.Save("out.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