【问题标题】:XElement multiple root elementsXElement 多个根元素
【发布时间】:2016-12-22 18:56:19
【问题描述】:

我有以下 XML 格式由 XElement 类创建

在其中你可以看到我有两个父节点Service和Catalog。现在,当我从下面的源代码中读取代码 sn-p

XElement rootElement = new XElement("Catalog", new XElement("Service"));//Create a root Element 

foreach (....)// Logic for multiple product element 

XElement productElement = new XElement("Product", new XAttribute("name", Obj.Product));//Read the Product
XElement variantElement = new XElement("Variant", new XAttribute("name",Obj.Variant));//Read variant
XElement skuElement = new XElement("SKU", new XAttribute("name", Obj.SKU));//Read SKU

productElement.Add(variantElement, skuElement);//Add varaint and skuvariant to product
rootElement.Add(productElement);//Add product to root element

但输出不是预期的,下面是输出

您可以看到第二个父节点在开头显示为闭包。 我知道这条线正在制造问题

XElement rootElement = new XElement("Catalog", new XElement("Service"));

但我需要两个根元素?解决办法是什么?如何创作?

请帮忙。

【问题讨论】:

  • 唯一的根元素是目录。 XML 文档可能包含多个根元素。另外,请将问题本身的 XML 和输出粘贴为代码(而不是图像)。

标签: c# xml xelement


【解决方案1】:

Service 元素将始终为空,因为您实际上从未向其中添加任何内容,您只是创建它。您将产品添加到 Catalog 元素,而不是将它们添加到 Service 元素。

此外,将这些“根”称为不正确的术语,因为有效的 XML 文档只能有一个根(因此在这种情况下,唯一的根元素是 Catalog)。

代码如下:

public class Product
    {
        public string ProductName { get; set; }

        public string VariantName { get; set; }

        public string SKU { get; set; }
    }

    private static void CreateDoc(List<Product> list)
    {
        XElement serviceElement = new XElement("Service");
        XElement rootElement = new XElement("Catalog", serviceElement);//Create a root Element 

        // Obviously include the foreach loop
        foreach (Product product in list)
        {
            // Obviously replace these hardcoded string with your actual object values
            XElement productElement = new XElement("Product", new XAttribute("name", product.ProductName));//Read the Product
            XElement variantElement = new XElement("Variant", new XAttribute("name", product.VariantName));//Read variant
            XElement skuElement = new XElement("SKU", new XAttribute("name", product.SKU));//Read SKU

            productElement.Add(variantElement, skuElement);//Add varaint and skuvariant to product

            // Add product to the Service element, NOT to the root. The root is Catalog.
            serviceElement.Add(productElement);
        }

        rootElement.Save("XmlFile.xml");
    }

主要变化是

// Store the Service element is a variable so we can add stuff to it
XElement serviceElement = new XElement("Service");

// ...

// Add to the Service element instead of to Catalog
serviceElement.Add(productElement);

我这样称呼它:

List<Product> list = new List<Product>
        {
            new Product { ProductName = "SomeName", SKU = "SomeSKU", VariantName = "VariantA" },
            new Product { ProductName = "AnotherProduct", SKU = "AnotherSKU", VariantName = "VariantB" },
            new Product { ProductName = "ProductC", SKU = "SKUc", VariantName = "VariantC" }
        };

        CreateDoc(list);

这是生成的 XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Catalog is the root element for this document -->
<Catalog>
  <!-- Service is NOT a root element, even though it's the only child of Catalog -->
  <Service>
    <!-- Each Service element can have an arbitrary number of Product elements -->
    <Product name="SomeName">
      <Variant name="VariantA" />
      <SKU name="SomeSKU" />
    </Product>
    <Product name="AnotherProduct">
      <Variant name="VariantB" />
      <SKU name="AnotherSKU" />
    </Product>
    <Product name="ProductC">
      <Variant name="VariantC" />
      <SKU name="SKUc" />
    </Product>
  </Service>
</Catalog>

另一个澄清 - 在这种情况下 Service 不是 根元素(即使在这种情况下它是 Catalog 的唯一子元素)。

【讨论】:

  • 感谢 EJoshuaS!上面的代码将为每个产品元素添加服务元素,但我的要求是作为父元素,即 ..... ,我知道格式良好的 XML 将只有根元素,但我需要两个...让我知道任何可能性或任何解决方案?..谢谢 troy
  • @Troy 我对其进行了编辑以使其更加清晰 - 每个服务可以有任意数量的产品。
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 2021-10-19
  • 2023-02-07
相关资源
最近更新 更多