Add subnodes to an existing node

G

Grant

Gday,

I am using the sample code at:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317666 to modify an
existing XML document. The XML document looks like the following:

---------------------------
<parameters>
<Config-File>
<Value>c:\config.xml</Value>
</Last-Config-File>
<Modules />
</parameters>
---------------------------

What I would like to do is add subnodes to the <Modules /> node so the XML
file would end up looking something like:

---------------------------
<parameters>
<Config-File>
<Value>c:\config.xml</Value>
</Last-Config-File>

<Modules>
<Module1>
<somevalue>Text here</somevalue>
</Module1>
<Module2>
<somevalue>Text here</somevalue>
</Module2>
</Modules>

</parameters>
---------------------------

The problem with the sample code on the MSDN site is that the 'AppendChild'
function adds the node as a child node of the documents root node - I need
to add it to the modules node!

I really am stuck with this and any assistance would be greatly appreciated.

Thanks,
Grant
 
N

Nicholas Paldino [.NET/C# MVP]

Grant,

Basically, you want to call the AppendChild method on the node that you
actually want to append the other values to. So, instead of calling it on
the DocumentElement, you should append it to the new element that you
appended to the document element (the modules element).

Hope this helps.
 
G

Grant

That was quick!
The problem is that the modules element already exists when I open the file,
and I dont know how to add new elements to an already exising one...



Nicholas Paldino said:
Grant,

Basically, you want to call the AppendChild method on the node that you
actually want to append the other values to. So, instead of calling it on
the DocumentElement, you should append it to the new element that you
appended to the document element (the modules element).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Grant said:
Gday,

I am using the sample code at:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317666 to modify
an existing XML document. The XML document looks like the following:

---------------------------
<parameters>
<Config-File>
<Value>c:\config.xml</Value>
</Last-Config-File>
<Modules />
</parameters>
---------------------------

What I would like to do is add subnodes to the <Modules /> node so the
XML file would end up looking something like:

---------------------------
<parameters>
<Config-File>
<Value>c:\config.xml</Value>
</Last-Config-File>

<Modules>
<Module1>
<somevalue>Text here</somevalue>
</Module1>
<Module2>
<somevalue>Text here</somevalue>
</Module2>
</Modules>

</parameters>
---------------------------

The problem with the sample code on the MSDN site is that the
'AppendChild' function adds the node as a child node of the documents
root node - I need to add it to the modules node!

I really am stuck with this and any assistance would be greatly
appreciated.

Thanks,
Grant
 
G

Guest

Try this

XmlNode rootNode = doc.DocumentElement;
XmlNode modulesNode= rootNode.SelectSingleNode ("Modules");
modulesNode.AppendChild(newElem);

Brian Strohl
 

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