Adding a XML Attribute for an Exiting Node

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
 
R

Randy Charles Morin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top