XElement多个根元素

时间:2016-12-22 18:56:19

标签: c# xml xelement

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

XML Source

在其中您可以看到我有两个父节点服务和目录。现在,当我从下面的源代码中读到代码片段

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

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

enter image description here

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

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

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

请帮忙。

1 个答案:

答案 0 :(得分: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的唯一子元素)。