473,498 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange question: Add same nodes to XmlDocument

Hi all,
i'm trying to add same node to XmlDocument.
This is Xml that I want to create:
<TableColumns>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
</TableColumns>

I tried to do this with XmlDocument method AppenChild but it remove
duplicate node. If it is possibile I dont want to use XmlTextWriter.
Any idea?

Thx
Marco
Nov 12 '05 #1
3 1654
If you want to clone your node, you can use XmlNode.CloneNode.

XmlNode tableColumnsNode = ...;
XmlNode copy = tableColumnsNode.CloneNode(/* deep */ true);
[parentNode].AppendChild(copy);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marco Rizzi" <Ma********@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
Hi all,
i'm trying to add same node to XmlDocument.
This is Xml that I want to create:
<TableColumns>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
</TableColumns>

I tried to do this with XmlDocument method AppenChild but it remove
duplicate node. If it is possibile I dont want to use XmlTextWriter.
Any idea?

Thx
Marco

Nov 12 '05 #2
I begin loading xml with one node TableColumn.
This code work but when I save I have only one node in my xml
....
string codexPath = "TableColumns/TableColumn";
XmlNode nodeTableCol = xmlTemp.SelectSingleNode(nodexPath);
for (int i = 0; i < 6; i++)
{
XmlNode copy = nodeTableCol.CloneNode(true);
nodeTableCol.ParentNode.AppendChild(copy);
}
....
xmlTemp.Save("c:\file.xml")

"Dennis Myrén" wrote:
If you want to clone your node, you can use XmlNode.CloneNode.

XmlNode tableColumnsNode = ...;
XmlNode copy = tableColumnsNode.CloneNode(/* deep */ true);
[parentNode].AppendChild(copy);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marco Rizzi" <Ma********@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
Hi all,
i'm trying to add same node to XmlDocument.
This is Xml that I want to create:
<TableColumns>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
</TableColumns>

I tried to do this with XmlDocument method AppenChild but it remove
duplicate node. If it is possibile I dont want to use XmlTextWriter.
Any idea?

Thx
Marco


Nov 12 '05 #3
You are selecting TableColumns/TableColumn:
string nodexPath = "TableColumns/TableColumn";
XmlNode nodeTableCol = xmlTemp.SelectSingleNode(nodexPath);

and you are running a SelectSingleNode on that expression, so all you will
get from that
query is the first:
<TableColumn><Width>1.5in</Width></TableColumn>

If you want to copy the entire <TableColumns /> you should select that node
and not it's first child.

string nodexPath = "TableColumns";
XmlNode nodeTableCol = xmlTemp.SelectSingleNode(nodexPath);

And, you will need to create a root element because if you would
have TableColumns as root and then add a new you would have 2 root elements.

So, assuming you have this XML(notice the root element):
<TableData>
<TableColumns>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
<TableColumn><Width>1.5in</Width></TableColumn>
</TableColumns>
</TableData>

....this snippet will do it:
XmlDocument doc = new XmlDocument();
doc.Load("C:\\file.xml");
XmlElement root = doc.DocumentElement;
XmlNode tableColumnsNode = root.SelectSingleNode("TableColumns");
XmlNode copy = tableColumnsNode.CloneNode(true);
root.AppendChild(copy);
doc.Save("C:\\file.xml");
HTH.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marco Rizzi" <Ma********@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
I begin loading xml with one node TableColumn.
This code work but when I save I have only one node in my xml
...
string codexPath = "TableColumns/TableColumn";
XmlNode nodeTableCol = xmlTemp.SelectSingleNode(nodexPath);
for (int i = 0; i < 6; i++)
{
XmlNode copy = nodeTableCol.CloneNode(true);
nodeTableCol.ParentNode.AppendChild(copy);
}
...
xmlTemp.Save("c:\file.xml")

"Dennis Myrén" wrote:
If you want to clone your node, you can use XmlNode.CloneNode.

XmlNode tableColumnsNode = ...;
XmlNode copy = tableColumnsNode.CloneNode(/* deep */ true);
[parentNode].AppendChild(copy);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marco Rizzi" <Ma********@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
> Hi all,
> i'm trying to add same node to XmlDocument.
> This is Xml that I want to create:
> <TableColumns>
> <TableColumn><Width>1.5in</Width></TableColumn>
> <TableColumn><Width>1.5in</Width></TableColumn>
> <TableColumn><Width>1.5in</Width></TableColumn>
> <TableColumn><Width>1.5in</Width></TableColumn>
> </TableColumns>
>
> I tried to do this with XmlDocument method AppenChild but it remove
> duplicate node. If it is possibile I dont want to use XmlTextWriter.
> Any idea?
>
> Thx
> Marco


Nov 12 '05 #4

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

Similar topics

1
3235
by: Vishal | last post by:
Hello, I am trying to a load a RSS with some really simple code. However when I run the code I got this error: '', hexadecimal value 0x1F, is an invalid character. Line 1, position 1. ...
2
10661
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
10
2269
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
0
1097
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
4
303
by: Wardeaux | last post by:
All, this XML is killing me... I'm new to XML, all I want to do is parse an XML string so that I can read the values... Mystr = "<A1><B1><C1>myC1</C1><C2>myC2</C2></B1><B2>myB2</B2><A1>" All...
4
1307
by: mattdaddym | last post by:
Hi all, I've taken a couple of hours to read what is available, and I still cannot figure out how to do a very simple task in vb .net...lol. All I need to do is read an xml file and parse out...
3
2611
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. ...
8
2037
by: =?Utf-8?B?TWFyaw==?= | last post by:
Here are three things that I thought would be equivalent but are not. Just wondering why: XmlElement foo = doc.CreateElement("foo"); // load it up with a body xslt.Transform (foo, null, new...
1
1688
by: =?Utf-8?B?RGF2aWRHQg==?= | last post by:
OK, so I've created and loaded an XMLDocument object. But how do I go about using it? Specifically, how do I: 1) move to the first node (I assume I start on it when I load the XML?) 2) move to...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7210
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...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
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
4595
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
3096
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...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
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.