Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hai all. I need to create an xml file like this ..

XML
<HEDDERS>
  <HEDDER>
    <ID>1</ID>
    <Name>ID</Name>
  </HEDDER>
  <HEDDER>
    <ID>6</ID>
    <Name>STATE</Name>
  </HEDDER>
  <HEDDER>
    <ID>7</ID>
    <Name>PIN</Name>
  </HEDDER>

</HEDDERS>



I use the code like this..

XML
XmlDocument docConfig = new XmlDocument();
       XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
       XmlElement rootElement = docConfig.CreateElement("HEDDERS");
       docConfig.AppendChild(rootElement);
       for (int i = 10; i < 20; i++)
       {
           // Create <InstallationId> Node
           XmlElement installationElement = docConfig.CreateElement("ID");
           XmlText installationIdText = docConfig.CreateTextNode(Convert.ToString(i));
           installationElement.AppendChild(installationIdText);
           docConfig.ChildNodes.Item(0).AppendChild(installationElement);

           // Create <Environment> Node
           XmlElement environmentElement = docConfig.CreateElement("NAME");
           XmlText environText = docConfig.CreateTextNode("ABC"+i);
           environmentElement.AppendChild(environText);
           docConfig.ChildNodes.Item(0).AppendChild(environmentElement);
       }



But it shows out put like this....

XML
<HEDDER>
  <ID>10</ID>
  <NAME>ABC10</NAME>
  <ID>11</ID>
  <NAME>ABC11</NAME>
  <ID>12</ID>
  <NAME>ABC12</NAME>
</HEDDER>


Can Any one help me to solve it out..
Posted
Comments
Hemant Singh Rautela 24-Dec-12 4:14am    
And What about for. how create this tag
xml version="1.0" encoding="utf-8"
srikanth492 17-Jun-14 14:20pm    
how to save xml file in current project folder..how to download that file ......can tell me

Hi, you missed creating the HEDDER nodes, that's why they're not appearing in the generated XML.

Modify your loop to something like:

C#
for (int i = 10; i < 20; i++)
{
    // Create the <hedder> Node
    XmlElement hedder = docConfig.CreateElement("HEDDER");

    // Create <installationid> Node
    XmlElement installationElement = docConfig.CreateElement("ID");
    XmlText installationIdText = docConfig.CreateTextNode(Convert.ToString(i));
    installationElement.AppendChild(installationIdText);

    // Add the <id> child node to the <hedder> element
    hedder.AppendChild(installationElement);

    // Create <environment> Node
    XmlElement environmentElement = docConfig.CreateElement("NAME");
    XmlText environText = docConfig.CreateTextNode("ABC" + i);
    environmentElement.AppendChild(environText);

    // Add the <name> child node to the <hedder> element
    hedder.AppendChild(environmentElement);

    // Add the HEDDER node to the root.
    rootElement.AppendChild(hedder);
}


I hope this helps.
 
Share this answer
 
v2
Comments
arun.m.mr 20-Jun-12 3:29am    
I need it like this.
<hedder>
<id>1
<name>ID


But This code shows like this..

<id>10
<name>ABC10
<hedder>
<id>11
<name>ABC11
<hedder>

No opening tag <HEADDER>... Only closing tag</HEADDER> is there..
markovl 20-Jun-12 3:39am    
It says 10, because that's how you've coded your loop - from 10 to 19.
You'll have to modify your code by yourself to suit your needs. I just pointed out that you missed to include the "HEDDER" element in your logic.

This code produced exactly the same structure as you've indicated that's intended.
C#
private void create()
    {
        
        XmlDocument docConfig = new XmlDocument();
        XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        XmlElement rootElement = docConfig.CreateElement("HEDDERS");
        docConfig.AppendChild(rootElement);
        
        for (int i = 10; i < 20; i++)
        {
            XmlElement hedder = docConfig.CreateElement("HEDDER");
            docConfig.DocumentElement.PrependChild(hedder);
            docConfig.ChildNodes.Item(0).AppendChild(hedder);
            // Create <installationid> Node
            XmlElement installationElement =docConfig.CreateElement("ID");
            XmlText installationIdText = docConfig.CreateTextNode(Convert.ToString(i));
            installationElement.AppendChild(installationIdText);
            hedder.PrependChild(installationElement);
            // Create <environment> Node
            XmlElement environmentElement = docConfig.CreateElement("NAME");
            XmlText environText = docConfig.CreateTextNode("ABC"+i);
            environmentElement.AppendChild(environText);
            
            hedder.PrependChild(environmentElement);
         
        }        

        // Save xml document to the specified folder path.
        docConfig.Save("D:\\Sample.xml");      
    }

This will give the out put like...

XML
<HEDDERS>
  <HEDDER>
    <NAME>ABC10</NAME>
    <ID>10</ID>
  </HEDDER>
  <HEDDER>
    <NAME>ABC11</NAME>
    <ID>11</ID>
  </HEDDER>
  <HEDDER>
    <NAME>ABC12</NAME>
    <ID>12</ID>
  </HEDDER>
  <HEDDER>
    <NAME>ABC13</NAME>
    <ID>13</ID>
  </HEDDER>
 
Share this answer
 
v2
Comments
markovl 20-Jun-12 6:17am    
And your point being what?

If the problem is with the actual values that are assigned to the different nodes are the names of the tags, adjust your code to reflect this.
For example the line:
docConfig.CreateTextNode("ABC" + i);
in your loop creates a text node and assigns its value to "ABCXX" ("XX" is the value of i at the current iteration).
Another thing, please do not post your additional questions as solutions, but use the "Improve question" option for your original question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900