2017-09-18 7 views
-2

Ich versuche, die Struktur eines sehr erweiterten XML zu ändern, indem ich zuerst ein Kind hinzufüge und danach versuche, das Elternteil des Knotens zu ändern. Ich bin nicht sehr erfahren in XML, also konnte ich das eine Kind hinzufügen, aber es scheint, dass, wenn ich versuche, das neue Elternteil anzuhängen, es nur alle Eltern löscht, die ich versuche zu ersetzen.C#: Wie ändert man die Struktur meines XML?

Hier ist die Gesamtstruktur des 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> 

Und hier ist, was ich versuche zu erreichen:

<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> 

Und hier ist der Code, den ich zusammen zu stellen versucht:

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(); 
+1

https://www.w3schools.com/xml/xsl_intro.asp –

Antwort

1

Sie erstellen das Element "Tabelle", dann fügen Sie das vorhandene Element zu diesem Element "Tabelle" hinzu. Da Sie jedoch das Element "table" nicht in Ihr Dokument einfügen, ist es verloren. Sie müssen AppendChild (table) für das Produktelement verwenden.

0

Sie können es so mit Linq xml tun:

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

     } 
    } 


} 
Verwandte Themen