Adding a XML Attribute for an Exiting Node

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi,
If I use the following code, there is a attribute.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item id='301'>Pink Ale Beer</item>");

If I code the 2nd line as below:
doc.LoadXml("<item>Pink Ale Beer</item>");
My question is how to add the attribute after that.
Please advice. Thanks.
Peter
 
Sample
static void Main(string[] args)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml("<item>Pink Ale Beer</item>");
doc.DocumentElement.SetAttribute("id", "301");
System.Console.WriteLine(doc.DocumentElement.OuterXml);
}

Hope this helps,

Randy
http://www.kbcafe.com
 
Back
Top