Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XMLdocument to add node

Tags:

c#

xml

In following XML document , I need to append a node

<DASHBOARD>
  <ANNOUNCEMENT>
    <DISPLAYTEXT>testin one</DISPLAYTEXT>
  </ANNOUNCEMENT>
  <ADMINLINKS>
    <LINK NAME="Google">"http:\\www.google.com"</LINK>
  </ADMINLINKS>
  <GENLINKS>
    <LINK NAME="Clearquest">"http://clearquest.com/cqweb/"</LINK>
    <LINK NAME="Google">http://www.google.com</LINK>
  </GENLINKS>
</DASHBOARD>

The issue is I need to add a new node named link under adminlinks and genlinks simultaneously. Here is the piece of code

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("DashBoard.xml");

XmlNode NodeGen = xmldoc.SelectSingleNode("DASHBOARD/GENLINKS");
XmlNode NodeAdmin = xmldoc.SelectSingleNode("DASHBOARD/ADMINLINKS");

XmlNode newLink = xmldoc.CreateNode(XmlNodeType.Element, "LINK", null);
XmlAttribute xa = xmldoc.CreateAttribute("NAME");
xa.Value = LinkName;
newLink.InnerText = Link;
newLink.Attributes.Append(xa);

NodeGen.AppendChild(newLink);
NodeAdmin.AppendChild(newLink);

xmldoc.Save("DashBoard.xml");

This is adding the link under adminlinks but not under genlinks.

like image 784
ppraj Avatar asked Mar 15 '11 12:03

ppraj


1 Answers

You're adding the new LINK node to the GENLINKS node, then moving it to ADMINLINKS. Try this instead:

NodeAdmin.AppendChild(newLink.Clone());
like image 185
Ian Hughes Avatar answered Oct 04 '22 06:10

Ian Hughes