DotNetDeveloper
DotNetDeveloper

Reputation: 587

more efficient approach in creating xml using Linq

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

Upvotes: 1

Views: 355

Answers (1)

bryanjonker
bryanjonker

Reputation: 3406

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

Upvotes: 2

Related Questions