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

发布于 2024-11-08 11:50:59 字数 1901 浏览 0 评论 0原文

我通过采用 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")));
            }
        }

I am creating xml by taking skelton xml and filling the nodes with values and if needed adding new nodes in between xml. I am doing in the following way and felt like ...not efficient.
Is there any better way of doing my requirement?

Sample Plain Product Xml:

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

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2024-11-15 11:50:59

如果“高效”是指“更易于维护”,您可能想尝试这种技术:

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

If by "efficient", you mean "easier to maintain", you may want to try this technique:

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

...and then you do this

        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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文