473,473 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

New parent node

Hi,

How do I add a new parent node to the existing nodes?
I have an existing XmlDocument object like this:

<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
.....

I want to add a <Products> node as its parent,

<Products>
<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
</Products>

Thanks.

david
Nov 12 '05 #1
2 1389
This might give you an idea of what you have to do:

XmlNode productsNode = doc.CreateNode(XmlNodeType.Element, "Products",
string.Empty);
XmlNodeList itemNodes = doc.SelectNodes("//item");
foreach (XmlNode itemNode in itemNodes)
{
productsNode.AppendChild(itemNode);
itemNode.ParentNode.RemoveChild(itemNode);
}
doc.DocumentElement.AppendChild(productsNode);

that is, first create the new <Products> node, then select all nodes that
you want to move into that node, loop through them and add each node to
<Products>, then remove them from their current location.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"David Lei" <we****@online.nospam> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Hi,

How do I add a new parent node to the existing nodes?
I have an existing XmlDocument object like this:

<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
....

I want to add a <Products> node as its parent,

<Products>
<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
</Products>

Thanks.

david

Nov 12 '05 #2
Thanks, I'll try it.
David
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:uO****************@TK2MSFTNGP10.phx.gbl...
This might give you an idea of what you have to do:

XmlNode productsNode = doc.CreateNode(XmlNodeType.Element, "Products",
string.Empty);
XmlNodeList itemNodes = doc.SelectNodes("//item");
foreach (XmlNode itemNode in itemNodes)
{
productsNode.AppendChild(itemNode);
itemNode.ParentNode.RemoveChild(itemNode);
}
doc.DocumentElement.AppendChild(productsNode);

that is, first create the new <Products> node, then select all nodes that
you want to move into that node, loop through them and add each node to
<Products>, then remove them from their current location.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"David Lei" <we****@online.nospam> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Hi,

How do I add a new parent node to the existing nodes?
I have an existing XmlDocument object like this:

<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
....

I want to add a <Products> node as its parent,

<Products>
<item>
<something>ABC</something>
<item>
<item>
<something>DEF</something>
<item>
</Products>

Thanks.

david


Nov 12 '05 #3

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

Similar topics

2
by: Nutshell | last post by:
Hi, I would like to add an attribute to the child node if it is the only child node. The problem is I am not getting the correct count of child nodes with this template: <?xml version="1.0"?>...
2
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the...
1
by: clintonG | last post by:
How do I get a TreeNode.Parent property when using the 2.0 TreeView control? When the data source is an XML file there may be redundant names in the tree. For example, when a child node with the...
3
by: Saurabh Sharma | last post by:
Hi When i select a node by "node.Selectnodes("*/ABC")" the nodes dont have ne details abt the parent. Can some one plz tell me why its is not storing the parent details. after selecting i need...
7
by: amruta | last post by:
the code below dows not let me get the parent child view... all the nodes are show in one line only... also i need them to be collasped ... Thanks ..
6
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
0
by: Guzeppi | last post by:
Hi, i'm using linq to load an xml structure into my classes. the xml consists of the same node nested for multiple levels e.g. <node id="node_id01" name="node 01"> <node id="node_id0101"...
1
by: bnchs | last post by:
This is C code. I am trying to fill each node's Parent field with its parent because I am drawing nodes to the screen. However, I have not been able to get this working. Reading the output from the...
4
by: jmDesktop | last post by:
I have searched everywhere and tried several things. I have a treeview with and want to be able to only select a parent node. For example: root //don't want to drag this -parent1 //yes, drag...
1
by: SWETA123 | last post by:
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this. Here is my problem. I have below (sample)xml. <Rules>...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.