在C Sharp中创建xml文档 [英] Creating xml document in c sharp

查看:70
本文介绍了在C Sharp中创建xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我必须创建一个XML文件,该文件将总共有98个节点.
如下所示:

Hi Everybody,

I have to create one xml file which will have total 98 nodes.
like below:

<root>
<child1>
<a></a>

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


注意:所有98个节点名称都不相同.它不像(row1,row2,row3 ..)

有人可以建议我任何解决方案/业务逻辑/通过c清晰的代码创建这个巨大的xml文件的步骤吗?

以下是一些节点名称:< ROOT>-< ASSETINFO>
< ASSETID>/ASSETID></MODEL</MODEL>< SERIALNUMBER>//SERIALNUMBER>< MANUFACTURER</MANUFACTURER>< EDSASSETTAG></ASSASSETTAG ;/CLIENTASSETTAG>< OTHERASSETTAG></OTHERASSETTAG>< SYSTEMNAME></SYSTEMNAME>< IPADDRESS></IPADDRESS>< SERVICEITEMDESCRIPTION></SERVICEITEM ;/ASSETINFO>-< SYSTEMCPUINFO></AFFECTEDITEM>< MANUFACTURER>//MANUFACTURER>< MODEL</MODEL< ASSETDESCRIPTION>< ;/SERIALNO>< EDSASSETTAG></EDSASSETTAG>< CLIENTASSETTAG></CLIENTASSETTAG>< OTHERASSETTAG></OTHERASSETTAG></SYSTEMCPUINFO>< CONTACTINFO> ;. ;</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>

推荐答案

下面是创建xml文件的一种方法:
我使用了XmlTextWriter 类,正如您所说的XML文件可能很重.
这是代码段,将其放在您想要的位置.

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.

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



您的xml文件将以.xml格式存储在C:驱动器中.

使用XmlTextReader 类读取它,该类通常用于读取大型XML文件.

希望对您有所帮助!



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!


Google中的第一个链接,用于在c#中创建xml文档"

http://paulsiu.wordpress.com/2007/04/04/creating-a-xml-document-from-scratch-without-using-using-a-file-in-c/ [
First link in Google for "Creating xml document in c#"

http://paulsiu.wordpress.com/2007/04/04/creating-a-xml-document-from-scratch-without-using-a-file-in-c/[^]


看看这个:创建C#中的XML树(LINQ to XML) [ ^ ]

它使用XElement类生成XML文件.
这段代码将生成类似这样的输出.
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

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



XElement的好处是您可以在其上构建越来越多的节点.
完成后,调用.Save("filename.xml");



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


这篇关于在C Sharp中创建xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆