码迷,mamicode.com
首页 > 其他好文 > 详细

create XML

时间:2014-09-25 16:35:49      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   on   c   ad   line   ef   r   

string line1 = "44";
string fas01 = "4";
string jj1 = "1路车发案4起,3月1日1起";

string line2 = "43";
string fas02 = "6";
string jj2 = "6路车发案4起,3月1日1起";

XmlDocument doc = new XmlDocument();
//创建XML声明部分
XmlDeclaration Declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(Declaration, doc.DocumentElement);
//创建根节点
XmlElement Root = doc.CreateElement("Root");//主内容
doc.AppendChild(Root);


XmlElement Child1 = doc.CreateElement("luduan");

XmlAttribute attr1 = doc.CreateAttribute("mingcheng");
attr1.Value = line1;
Child1.Attributes.Append(attr1);

XmlElement fas1 = doc.CreateElement("fas");
fas1.InnerText = fas01;
Child1.AppendChild(fas1);

XmlElement jianjie = doc.CreateElement("jianjie");
jianjie.InnerText = jj1;
Child1.AppendChild(jianjie);

Root.AppendChild(Child1);

Child1 = doc.CreateElement("luduan");
attr1 = doc.CreateAttribute("mingcheng");
attr1.Value = line2;
Child1.Attributes.Append(attr1);

fas1 = doc.CreateElement("fas");
fas1.InnerText = fas02;
Child1.AppendChild(fas1);

jianjie = doc.CreateElement("jianjie");
jianjie.InnerText = jj2;
Child1.AppendChild(jianjie);

Root.AppendChild(Child1);


string path = Server.MapPath("~");
string fullpath = path + "\\Save\\" + "1.xml";
doc.Save(fullpath);


string xmlFileName = Server.MapPath("Save/1.xml");

//
StreamReader sr = new StreamReader(xmlFileName, System.Text.Encoding.Default);

string xmlData = sr.ReadToEnd();

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xmlData);
//
doc2.Save("C://2.XML");

create XML

标签:io   ar   for   on   c   ad   line   ef   r   

原文地址:http://www.cnblogs.com/gongyu/p/3992642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!