473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Add subnodes to an existing node

Gday,

I am using the sample code at:
http://support.microsoft.com/default...b;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
Nov 17 '05 #1
4 1778
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]
- mv*@spam.guard.caspershouse.com

"Grant" <gp*****@hotmail.com> wrote in message
news:eY*************@TK2MSFTNGP15.phx.gbl...
Gday,

I am using the sample code at:
http://support.microsoft.com/default...b;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

Nov 17 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Grant" <gp*****@hotmail.com> wrote in message
news:eY*************@TK2MSFTNGP15.phx.gbl...
Gday,

I am using the sample code at:
http://support.microsoft.com/default...b;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


Nov 17 '05 #3
Try this

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

Brian Strohl


"Grant" wrote:
Gday,

I am using the sample code at:
http://support.microsoft.com/default...b;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

Nov 17 '05 #4
I could have sworn I tried that! That works now.

Thank you for your help.
Grant

"Brian Strohl" <Brian St****@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Try this

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

Brian Strohl


"Grant" wrote:
Gday,

I am using the sample code at:
http://support.microsoft.com/default...b;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

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kristofer Andersson | last post by:
I am using xslt to transform some parts of a document but want to output some parts of the document just like they are (node with all subnodes and attributes). Below are the templates I am...
1
by: Johan Åhlén | last post by:
Hi, Does anybody have any idea how to access child nodes in a TreeView without using postbacks? For example: in the subtree below I want to disable nodes B, C and D when A is checked (and...
0
by: Limno | last post by:
Hi, I have a treeview control with showcheckboxes = All in my C#.net web application. When i check on the child node its parent node should be checked automatically.similarlly when i uncheck...
3
by: raghudr | last post by:
Hi all, I am parsing a .xml file.My main intention is to retrieve the name value of node "Signal":- "Name Value" which is "rag". i want to store only the <signal"name value" that is only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.