C# : How to modify the structure of my xml?

117 Views Asked by At

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();
2

There are 2 best solutions below

0
On

You can do it with linq xml like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication5
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> products = doc.Descendants("product").ToList();
            foreach (XElement product in products)
            {
                product.Add(new XElement("product", new XAttribute("id", (string)product.Attribute("id"))));

                product.ReplaceWith(new XElement("table", product.Descendants()));
            }

        }
    }


}
0
On

You create the element "table", then you add the existing item to this "table" element. But since you are not inserting the "table" element in your document it is lost. You must use AppendChild (table) on the products element.