(转)创建XML文件添加元素信息
1
public void CreateXML()
2
{
3
XmlTextWriter writer = new XmlTextWriter("userInfo.xml", Encoding.UTF8);//创建一个名为userInfo的XML文件
4![](/Images/OutliningIndicators/InBlock.gif)
5
writer.WriteStartDocument();//根据创建文件时的文件编码创建一个版本信息(如:<?xml version="1.0" encoding="utf-8"?>)
6
writer.WriteStartElement("userInfo");//创建一个名为userinfo的开始元素结点
7![](/Images/OutliningIndicators/InBlock.gif)
8
writer.WriteStartElement("browserInfo");//创建一个名为browserInfo的开始元素结点
9
writer.WriteElementString("urlReferrer", "none");//创建一个名为urlReferrer的browserInfo的子元素内容为"none"
10
writer.WriteElementString("userAgent", "aaa");
11
writer.WriteElementString("userLanguages", "bbb");
12
writer.WriteEndElement();//创建browserInfo的结束元素结点
13![](/Images/OutliningIndicators/InBlock.gif)
14
writer.WriteStartElement("visitInfo");//创建一个名为visitInfo的开始元素结点
15
writer.WriteAttributeString("timeVisited", DateTime.Now.ToString());//创建一个visitInfo元素结点的timeVisited属性内容为当前日期
16
writer.WriteElementString("ip", "ccc");
17
writer.WriteElementString("rawUrl", "ddd");
18
writer.WriteEndElement();//创建visitInfo的结束元素结点
19![](/Images/OutliningIndicators/InBlock.gif)
20
writer.WriteEndElement();//创建userinfo的结束元素结点
21
writer.WriteEndDocument();
22
writer.Close();
23
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)