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