导航

xml

Posted on 2010-02-20 16:23  地火明夷  阅读(127)  评论(0)    收藏  举报
<?xml version="1.0" encoding="gb2312"?>
<rootEle>
  <Node>
    <ele1 Attribute1="abc" Attribute2="123">one</ele1>
    <ele2>two</ele2>
    <ele3>three</ele3>
  </Node>
  <Node>
    <ele1 Attribute1="abc" Attribute2="123">one</ele1>
    <ele2>two</ele2>
    <ele3>three</ele3>
  </Node>
</rootEle>
xml生成
 1 private void WriteXml()
 2 {
 3     XmlDocument xmldoc;
 4     XmlNode xmlnode;
 5     XmlElement xmlelem;
 6 
 7     xmldoc = new XmlDocument();
 8     //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
 9     XmlDeclaration xmldecl;
10     xmldecl = xmldoc.CreateXmlDeclaration("1.0""gb2312"null);
11     xmldoc.AppendChild(xmldecl);
12 
13     //加入一个根元素
14     xmlelem = xmldoc.CreateElement("""rootEle""");
15     xmldoc.AppendChild(xmlelem);
16     //加入另外一个元素
17     for (int i = 1; i < 3; i++)
18     {
19 
20         XmlNode root = xmldoc.SelectSingleNode("rootEle");//查找<root> 
21 
22         xmlnode = xmldoc.CreateElement("Node");//创建一个<Node>节点 
23         
24 
25         XmlElement xesub1 = xmldoc.CreateElement("ele1");
26         xesub1.SetAttribute("Attribute1""abc");//设置该节点属性 
27         xesub1.SetAttribute("Attribute2""123");
28         xesub1.InnerText = "one";//设置文本节点 
29         xmlnode.AppendChild(xesub1);//添加到<Node>节点中 
30         XmlElement xesub2 = xmldoc.CreateElement("ele2");
31         xesub2.InnerText = "two";
32         xmlnode.AppendChild(xesub2);
33         XmlElement xesub3 = xmldoc.CreateElement("ele3");
34         xesub3.InnerText = "three";
35         xmlnode.AppendChild(xesub3);
36 
37         root.AppendChild(xmlnode);//添加到<根>节点中 
38     }
39     //保存创建好的XML文档
40     xmldoc.Save(Server.MapPath("data.xml"));
41 }