Editing Xml

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm pretty new with XML, and I need to edit a XmlDocument in C#....

now here is an example on what I need.

I have this XMLDocument:

<?xml version="1.0"?>
<root>
<name>John Doe</name>
<conf class="7" uniqueid="1" password="0" />
<userlist>
<users id="1" userid="2" cardid="2"/>
</userlist>
</root>

Ok this is it.... you see the " <users id="1" userid="2" cardid="2"/>"...
that part is supposed to be added by me on the code... and I can't... I would
appreciate some help, hints and/or code snippet to see how can I do it.

Thanks in Advance
 
Diogo Alves - Software Developer
I'm pretty new with XML, and I need to edit a XmlDocument in C#....

now here is an example on what I need.

I have this XMLDocument:

<?xml version="1.0"?>
<root>
<name>John Doe</name>
<conf class="7" uniqueid="1" password="0" />
<userlist>
<users id="1" userid="2" cardid="2"/>
</userlist>
</root>

Ok this is it.... you see the " <users id="1" userid="2" cardid="2"/>"...
that part is supposed to be added by me on the code... and I can't... I would
appreciate some help, hints and/or code snippet to see how can I do it.

Well, first create an XmlDocument instance, then load the existing XML,
then find the element you wish to add something into (in this case the
userlist element), then create and add the new element (using
XmlDocument.CreateElement and XmlNode.AppendChild respectively). Set
the attributes you want on the element using XmlElement.SetAttribute.

I would suggest using XPath to find the userlist element - use
XmlNode.SelectSingleNode on the root node of the element, passing in
"/root/userlist" as the XPath expression.
 

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

Back
Top