I am trying to modify the structure of an very extended xml by first adding one more child and afterwards trying to change the parent of the node .I am not very experienced in XML so I was able to add the one child , but it seems that when i'm trying to append the new parent , it just removes all the parents that i'm trying to replace.
Here is the overall structure of the XML :
<root>
<products>
<product id="1010-1001">
<name>
</name>
<unit>Styk</unit>
<shortDescription />
<longDescription>
</longDescription>
<currency>DKK</currency>
<price>
</price>
<categories>
<category>0912</category>
</categories>
</product>
<product id="1010-1002">
<name>
</name>
<unit>Styk</unit>
<shortDescription />
<longDescription>
</longDescription>
<currency>DKK</currency>
<price>31.982115219</price>
<categories>
<category>0912</category>
</categories>
</product>
</products>
</root>
And here's what I'm trying to achieve:
<root>
<products>
<table>
<name>BTS pulver 50 g m/antibiotika</name>
<Image>.</Image>
<longDescription>
</longDescription>
<product id="1010-1001" />
<shortDescription />
<unit>Styk</unit>
<price>10.6600000000000000000000</price>
</table>
</products>
</root>
And here is the code that I tried to put together :
XmlNodeList list = doc.SelectNodes("root/products/product");
foreach (XmlNode item in list)
{
XmlElement Image = doc.CreateElement("Image");
Image.InnerText = "id";
item.AppendChild(Image);
}
foreach(XmlNode parent in list)
{
XmlElement table = doc.CreateElement("table");
parent.ParentNode.RemoveChild(parent);
table.AppendChild(parent);
}
doc.Save(Console.Out);
Console.ReadKey();
You can do it with linq xml like this :