如何使用C#创建XML格式 [英] How To create XML Format using C#

查看:86
本文介绍了如何使用C#创建XML格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



任何budy可以告诉我如何创建xml文件,如下面给出的例子:



< etaalup_count>

< countnodes serviceid =A022114704152count =10/>

< countnodes serviceid =A022114704153count = 15/>

< countnodes serviceid =A022114704154count =5/>

< / etaalup_count>



谢谢和问候

Hussain

Hello Friends,

can any budy tell me that how to create xml file as given example below :

<etaalup_count>
<countnodes serviceid="A022114704152" count="10" />
<countnodes serviceid="A022114704153" count="15" />
<countnodes serviceid="A022114704154" count="5" />
</etaalup_count>

Thanks & Regards
Hussain

推荐答案

只需使用 Google ,你可以在网上找到很多教程。

例如,尝试阅读本文: C#XmlWriter [< a href =http://www.dotnetperls.com/xmlwritertarget =_ blanktitle =New Window> ^ ]。
Just using Google, you may find many tutorials on the web.
For instance, try reading this one: C# XmlWriter[^].


一个非常基本的表格让你前进,你可以根据需要改进代码。





A very basic form that get you going and you can improve the code as you want.


static void CreateXML()
{
    XmlDocument doc = new XmlDocument();

    XmlNode rootNode = doc.CreateElement("etaalup_count");
    doc.AppendChild(rootNode);

    XmlNode countNode = doc.CreateElement("countnodes");
    XmlAttribute serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704152";
    countNode.Attributes.Append(serviceAttribute);
    XmlAttribute countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "10";
    countNode.Attributes.Append(countAttribute);
    rootNode.AppendChild(countNode);

    countNode = doc.CreateElement("countnodes");
    serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704153";
    countNode.Attributes.Append(serviceAttribute);
    countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "15";
    countNode.Attributes.Append(countAttribute);
    rootNode.AppendChild(countNode);

    countNode = doc.CreateElement("countnodes");
    serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704154";
    countNode.Attributes.Append(serviceAttribute);
    countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "5";
    countNode.Attributes.Append(countAttribute);

    rootNode.AppendChild(countNode);
    doc.Save(Console.Out);
}


XmlWriter类的WriteChars方法将字符写入XML。它需要一个字符数组并一次写入一个字符。



以下代码片段采用一系列字符并将它们写入XML文件。



使用(XmlWriter writer = XmlWriter.Create(M.xml)){

writer.WriteStartDocument();



char [] ch = new char [6];

ch [0] ='m';

ch [1] ='a';

ch [2] ='h';

ch [3] ='e';

ch [4] ='s';

ch [5] ='h';



writer.WriteStartElement(WriteChars);



writer.WriteChars(ch,0,ch .Length);



writer.WriteEndElement();



writer.WriteEndDocument();



}
The WriteChars method of the XmlWriter class writes characters to XML. It takes an array of characters and writes one character at a time.

The following code snippet takes an array of characters and writes them to an XML file.

using (XmlWriter writer = XmlWriter.Create("M.xml")) {
writer.WriteStartDocument();

char[] ch = new char[6];
ch[0] = 'm';
ch[1] = 'a';
ch[2] = 'h';
ch[3] = 'e';
ch[4] = 's';
ch[5] = 'h';

writer.WriteStartElement("WriteChars");

writer.WriteChars(ch, 0, ch.Length);

writer.WriteEndElement();

writer.WriteEndDocument();

}


这篇关于如何使用C#创建XML格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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