create XML

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

转载于:https://www.cnblogs.com/gongyu/p/3992642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值