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