使用Linq创建xml的更有效方法

时间:2011-05-19 18:02:50

标签: linq-to-xml

我通过获取skelton xml并使用值填充节点来创建xml,如果需要,在xml之间添加新节点。我是按照以下方式做的,感觉......效率不高。 有没有更好的方法来满足我的要求?

样本普通产品Xml:

<root>
  <ProductInformation>
    <Name></Name>
    <ProductId></ProductId>
    <Description></Description>
    <Details>
      <TabName></TabName>
      <DetailList>
        <Add>
          <Type></Type>
          <Information></Information>
        </Add>
      </DetailList>
    </Details>
  </ProductInformation>
</root>

代码:

XDocument xmlDoc = XDocument.Load(Server.MapPath("/PlainProduct.xml"));
        xmlDoc.Element("root").Element("ProductInformation").SetElementValue("Name","iPhone");
        xmlDoc.Element("root").Element("ProductInformation").SetElementValue("ProductId", "123456");
        foreach (XElement detail in xmlDoc.Descendants("ProductInformation").Elements("Details"))
        {
            detail.SetElementValue("TabName", "Specifications");
            foreach (XElement x in detail.Elements("DetailList"))
            {
                //Update node value and here the node already exists in plain skelton xml
                x.Element("Add").SetElementValue("Type","Plastic");
                x.Element("Add").SetElementValue("Information", "24 Carat");
                //Added below two new nodes dynamically if my backend has more items of details programmatically
                x.Add(new XElement("Add",
                          new XElement("Type", "Fabric"),
                          new XElement("Information", "Smooth")));
                x.Add(new XElement("Add",
                         new XElement("Type", "Finishes"),
                         new XElement("Information", "Rogh")));
            }
        }

1 个答案:

答案 0 :(得分:2)

如果“有效”,则表示“更容易维护”,您可能需要尝试这种技术:

    static void SetItem(string XMLInformation, string newText, XElement document)
    {
        ((XElement)document.DescendantNodes().Last(x => 
               x.NodeType == System.Xml.XmlNodeType.Element
                 && ((XElement)x).Name == XMLInformation)).SetValue(newText);
    }

    static void SetElementItem(string XMLInformation, string elementItem, string newText, XElement document)
    {
        ((XElement)document.DescendantNodes().Last(x => 
               x.NodeType == System.Xml.XmlNodeType.Element
                 && ((XElement)x).Name == XMLInformation)).SetElementValue(elementItem, newText);
    }

    static void AddNewElement(string XMLInformation, string elementItem, XElement document)
    {
        ((XElement)document.DescendantNodes().Last(x => 
               x.NodeType == System.Xml.XmlNodeType.Element
                 && ((XElement)x).Name == XMLInformation)).Add(new XElement(elementItem);
    }

...然后你这样做

        var xmlDoc = XElement.Load("XmlFile.xml");
        SetItem("Name", "iPod", xmlDoc);
        SetItem("ProductId", "123456", xmlDoc);
        SetElementItem("Add", "Type", "Plastic", xmlDoc);
        SetElementItem("Add", "Information", "24 Carat", xmlDoc);

        //loop as needed
        AddNewElement("DetailList", "Add", xmlDoc);
        SetElementItem("Add", "Type", "Fabric", xmlDoc);
        SetElementItem("Add", "Information", "Rogh", xmlDoc);