Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everybody,

I have to create one xml file which will have total 98 nodes.
like below:
XML
<root>
<child1>
<a></a>

..
</child1>
<child2>
<c></c>
<d></d>
..
</child2>
....
</root>

Note:All 98 node names are different.Its not like (row1,row2,row3..)

Can anybody suggest me any solution/business logic/steps to create this huge xml file by c sharp code?

Below are some node names: <ROOT>- <ASSETINFO>
<ASSETID></ASSETID><MODEL></MODEL><SERIALNUMBER></SERIALNUMBER><MANUFACTURER></MANUFACTURER><EDSASSETTAG></EDSASSETTAG><CLIENTASSETTAG></CLIENTASSETTAG><OTHERASSETTAG></OTHERASSETTAG><SYSTEMNAME></SYSTEMNAME><IPADDRESS></IPADDRESS><SERVICEITEMDESCRIPTION></SERVICEITEMDESCRIPTION><OPERATINGSYSTEM></OPERATINGSYSTEM></ASSETINFO>- <SYSTEMCPUINFO><AFFECTEDITEM></AFFECTEDITEM><MANUFACTURER></MANUFACTURER><MODEL></MODEL><ASSETDESCRIPTION></ASSETDESCRIPTION><SERIALNO></SERIALNO><EDSASSETTAG></EDSASSETTAG><CLIENTASSETTAG></CLIENTASSETTAG><OTHERASSETTAG></OTHERASSETTAG></SYSTEMCPUINFO>- <CONTACTINFO>..... </CONTACTINFO></ROOT>
Posted
Updated 12-Apr-11 4:11am
v4
Comments
musefan 12-Apr-11 9:45am    
Well what is it that determines the nodes in the XML?
Tarun.K.S 12-Apr-11 10:14am    
Please post the Xml nodes properly. Its unreadable.

Take a look at this: Creating XML Trees in C# (LINQ to XML)[^]

It uses XElement class to generate a XML file.
This code will generate an output like this one.
XElement address = new XElement("Address",
    new XElement("Street1", "123 Main St"),
    new XElement("City", "Mercer Island"),
    new XElement("State", "WA"),
    new XElement("Postal", "68042")
);
Console.WriteLine(address);


Output

XML
<Address>
  <Street1>123 Main St</Street1>
  <City>Mercer Island</City>
  <State>WA</State>
  <Postal>68042</Postal>
</Address>


The nice thing with XElement is that you just build more and more nodes on it.
And when you are done, call .Save("filename.xml");
 
Share this answer
 
v2
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 12:27pm    
Useful article to the purpose. My 5.
--SA
Here is a way to create an xml file :
I have used an XmlTextWriter class as you said the XML file can be heavy.
Here is the code snippet, put it where you want to.

C#
String filePath = @"C:\Whatis.xml";
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
XmlTextWriter writer = new XmlTextWriter(fs, System.Text.Encoding.UTF8);

writer.Formatting = Formatting.Indented; //Used so that it can be easily viewed in notepad

writer.WriteStartDocument(true);
writer.WriteStartElement("root");

for (int i = 1; i <= 98; i++)
{
    writer.WriteStartElement("child" + i.ToString());
    writer.WriteStartElement("a" + i.ToString());
    writer.WriteEndElement(); //close the "a" element
    writer.WriteEndElement(); //close the "child" element
}
writer.WriteEndElement(); //closes the "root" element
writer.Close();
fs.Close();


Your xml file will be stored in the C: drive in .xml format.

Read it using XmlTextReader class which is generally used for reading large XML files.

Hope it helped!
 
Share this answer
 
v2
Comments
Sugato Pal 12-Apr-11 10:06am    
Thanks Tarun .But my problem is there is no match in the node names.I already mentioned it won't be same.Below are some node names:
<root>- <assetinfo><assetid><model><serialnumber><manufacturer><edsassettag><clientassettag><otherassettag><systemname><ipaddress><serviceitemdescription><operatingsystem>- <systemcpuinfo><affecteditem><manufacturer><model><assetdescription><serialno><edsassettag><clientassettag><otherassettag>- <contactinfo>..... To get this format i thik for loop will not serve.
Sergey Alexandrovich Kryukov 12-Apr-11 12:26pm    
Pay attention to the Answer!
XmlTextWriter and XmlTextReader can do anything at all. It's up to you and application logic to tackle different nodes. I see no problem.
--SA
BobJanova 12-Apr-11 10:41am    
If they are all different and follow no pattern then you will have to write them all individually. If the names are all different but the data follows a pattern then you can store the names in a list and loop through that.
Sugato Pal 12-Apr-11 10:51am    
Yes you are right..Thanks
Sergey Alexandrovich Kryukov 12-Apr-11 12:26pm    
My 5.
--SA

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