Asp.net XML心得3:写xml
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 XmlWriterSettings settings = new XmlWriterSettings();
4 settings.Indent = true;
5 settings.IndentChars = "\t";
6 settings.OmitXmlDeclaration = false;
7 using (XmlWriter writer = XmlWriter.Create(Server.MapPath("writer.xml"),settings))
8 {
9 writer.WriteStartDocument(true);
10 writer.WriteComment("This is my first asp.net to xml. ");
11 writer.WriteStartElement("exployees");
12 writer.WriteStartElement("exployee"); writer.WriteAttributeString("id", "1");
13 writer.WriteStartElement("name");
14 writer.WriteElementString("firstName", "L");
15 writer.WriteElementString("lastName", "WY");
16 writer.WriteEndElement();
17
18 writer.WriteElementString("city", "xz");
19 writer.WriteElementString("state","abcd");
20 writer.WriteElementString("zipcode","221000");
21 writer.WriteEndElement();
22 writer.WriteEndElement();
23 writer.WriteEndDocument();
24 }
25 }
2 {
3 XmlWriterSettings settings = new XmlWriterSettings();
4 settings.Indent = true;
5 settings.IndentChars = "\t";
6 settings.OmitXmlDeclaration = false;
7 using (XmlWriter writer = XmlWriter.Create(Server.MapPath("writer.xml"),settings))
8 {
9 writer.WriteStartDocument(true);
10 writer.WriteComment("This is my first asp.net to xml. ");
11 writer.WriteStartElement("exployees");
12 writer.WriteStartElement("exployee"); writer.WriteAttributeString("id", "1");
13 writer.WriteStartElement("name");
14 writer.WriteElementString("firstName", "L");
15 writer.WriteElementString("lastName", "WY");
16 writer.WriteEndElement();
17
18 writer.WriteElementString("city", "xz");
19 writer.WriteElementString("state","abcd");
20 writer.WriteElementString("zipcode","221000");
21 writer.WriteEndElement();
22 writer.WriteEndElement();
23 writer.WriteEndDocument();
24 }
25 }
输出结果:
1 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
2 <!--This is my first asp.net to xml. -->
3 <exployees>
4 <exployee id="1">
5 <name>
6 <firstName>L</firstName>
7 <lastName>WY</lastName>
8 </name>
9 <city>xz</city>
10 <state>abcd</state>
11 <zipcode>221000</zipcode>
12 </exployee>
13 </exployees>
2 <!--This is my first asp.net to xml. -->
3 <exployees>
4 <exployee id="1">
5 <name>
6 <firstName>L</firstName>
7 <lastName>WY</lastName>
8 </name>
9 <city>xz</city>
10 <state>abcd</state>
11 <zipcode>221000</zipcode>
12 </exployee>
13 </exployees>