Search code examples
asp.netxmlxmldocument

How to create node in loop in XmlDocument using ASP.Net C#?


I am trying to create Xml file. I want to create node in loops

  1. Below code what i make it to create

     XDocument doc = new XDocument(           
         new XElement("Bill",
             new XElement("ITEM",
                 new XElement("Mat",
                     new XElement("ID","1"),
                     new XElement("Code"),
                     new XElement("Name")
                 ),//Mat
                 new XElement("Store",
                     new XElement("ID","1"),
                     new XElement("Code"),
                     new XElement("Name")
                 )
             )//ITEM
         )//Bill
     );
    
     doc.save("Test.xml");
    

Output

<Bill>
    <Version>1.0</Version>
    <ITEM>
        <Mat>
            <ID>1<ID/>
            <Code />
            <Name />
        </Mat>
        <Store>
            <ID>1<ID/>
            <Code />
            <Name />
       </Store>
    </ITEM>
</Bill>

I need to be like below Output

<Bill>
    <Version>1.0</Version>
    <ITEM>
        <Mat>
            <ID>1<ID/>
            <Code />
            <Name />
        </Mat>
        <Store>
            <ID>1<ID/>
            <Code />
            <Name />
       </Store>
    </ITEM>
    <ITEM>
        <Mat>
            <ID>2<ID/>
            <Code />
            <Name />
        </Mat>
        <Store>
            <ID>2<ID/>
            <Code />
            <Name />
       </Store>
    </ITEM>
    <ITEM>
        <Mat>
            <ID>3<ID/>
            <Code />
            <Name />
        </Mat>
        <Store>
            <ID>3<ID/>
            <Code />
            <Name />
       </Store>
    </ITEM>
</Bill>

Note

I will get the value from db and pass it to xml nodes of (ID,Code,Name). I want to iterate in loop..

Solution

  • Here is image of results

    enter image description here

    You are using XDocument (not xmlDocument) which is xml linq. Try following :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
    
            static void Main(string[] args)
            {
                string ident = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><BILL></BILL>";
                XDocument doc = XDocument.Parse(ident);
    
                XElement bill = doc.Root;
                bill.Add(new XElement("version","1.0"));
    
                for (int i = 1; i <= 3; i++)
                {
                    XElement item = new XElement("ITEM");
                    bill.Add(item);
    
                    item.Add(new XElement("Mat", new object[] {
                        new XElement("ID",i),
                        new XElement("Code"),
                        new XElement("Name")
                    }));
    
                    item.Add(new XElement("Store", new object[] {
                        new XElement("ID",i),
                        new XElement("Code"),
                        new XElement("Name")
                    }));
                }
            }
        }
        
    
    }