Creating XmlAttribute s


Creating XmlAttribute s

To create an XmlAttribute , the XmlDocument provides the CreateAttribute method. This method creates an XmlAttribute node linked to the current XmlDocument . The value must be set after the XmlAttribute is created (see Listing 11.18).

Listing 11.18
 C# XmlAttribute newAttribute = XmlDoc.CreateAttribute("att"); newAttribute.Value = "val"; VB Dim newAttribute As XmlDoc.CreateAttribute("att") newAttribute.Value = "val" 

After creating the attribute, you must add it to an XmlElement . The XmlElement 's SetAttributeNode method provides this functionality. The SetAttributeNode takes the newly created XmlAttribute and adds it to the XmlElement 's list of attributes. The SetAttributeNode method has an overload that takes two parameters: one string representing the name of the attribute, and another string representing the namespace URI of the attribute. This overload returns a new XmlAttribute that is already attached to the XmlElement and the XmlElement 's owner document. The XmlAttribute will not have a value, so you must set the Value property after calling this method. Listing 11.19 demonstrates how to add a new XmlAttribute to an XmlElement by using SetAttributeNode.

Listing 11.19
 C# XmlAttribute newAttribute = element.SetAttributeNode("att", null); newAttribute.Value = "val"; VB Dim newAttribute As element.SetAttributeNode("att", Nothing) newAttribute.Value = "val" 

An alternate way of adding a method is the XmlElement 's AddAttribute method. This method is very convenient . It creates an XmlAttribute , adds the XmlAttribute to the XmlElement , sets the XmlAttribute 's owner document, and then sets the XmlAttribute 's value (see following code snippet).

 
 C# element.SetAttribute("title", "Going Back to Cali"); VB element.SetAttribute("title", "Going Back to Cali"); 


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