【问题标题】:C# : How to modify the structure of my xml?C#:如何修改我的 xml 的结构?
【发布时间】:2018-02-27 08:14:40
【问题描述】:

我正在尝试通过首先添加一个子节点然后尝试更改节点的父节点来修改非常扩展的 xml 的结构。我对 XML 不是很有经验,所以我能够添加一个子节点,但是似乎当我试图附加新的父级时,它只是删除了我试图替换的所有父级。

以下是 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>

这就是我想要实现的目标:

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

这是我尝试整理的代码:

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

【问题讨论】:

标签: c# xml xmldocument xml-documentation


【解决方案1】:

您创建元素“table”,然后将现有项目添加到此“table”元素。但是由于您没有在文档中插入“表格”元素,因此它会丢失。您必须在 products 元素上使用 AppendChild(表格)。

【讨论】:

    【解决方案2】:

    你可以像这样使用 linq xml:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2018-05-23
      • 1970-01-01
      • 2012-12-12
      相关资源
      最近更新 更多