Chinaunix首页 | 论坛 | 博客
  • 博客访问: 380719
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1741
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-21 22:46
文章分类
文章存档

2014年(22)

2013年(45)

分类: C#/.net

2014-01-02 17:43:32

有些时候我们需要生成一个xml文档作为数据交换的容器。当然我们用拼接字符串的方法来进行构建xml,但是这种方法虽然简单有效,但是如果xml文档结构过于复杂,拼接字符串会让人眼花缭乱。这时候就需要C#给我们提供现成的类库,以供我们自由的创建xml文档。

比如我们要创建如下的xml文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    

   
    ZFM1
   
        13022101
        2013238955
        4140
   

   
        13022101
        2013239627
        4140
   



选取这样的结构,一方面是因为它来自于论坛某位坛友实际的需求,另一方面它足够简单却有代表性。

下面我将以这个例子用两种方法(XmlDocument和Linq to XML)进行讲解。

1、XmlDocument
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    
XmlDocument document = new XmlDocument();
              
XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
document.AppendChild(declaration);
              
XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "");
document.AppendChild(root);
              
XmlElement zwerks = document.CreateElement("ZWERKS");
zwerks.InnerText = "ZFM1";
root.AppendChild(zwerks);
              
XmlElement tab1 = document.CreateElement("TAB1");
root.AppendChild(tab1);
              
XmlElement zno = document.CreateElement("ZNO");
zno.InnerText = "13022101";
tab1.AppendChild(zno);
              
XmlElement zorder = document.CreateElement("ZORDER");
zorder.InnerText = "2013238955";
tab1.AppendChild(zorder);
              
XmlElement zweight = document.CreateElement("ZWEIGHT");
zweight.InnerText = "4140";
tab1.AppendChild(zweight);
              
XmlElement tab2 = document.CreateElement("TAB1");
root.AppendChild(tab2);
              
XmlElement zno2 = document.CreateElement("ZNO");
zno2.InnerText = "13022101";
tab2.AppendChild(zno2);
              
XmlElement zorder2 = document.CreateElement("ZORDER");
zorder2.InnerText = "2013238955";
tab2.AppendChild(zorder2);
              
XmlElement zweight2 = document.CreateElement("ZWEIGHT");
zweight2.InnerText = "4140";
tab2.AppendChild(zweight2);
              
document.Save("test.xml");//将生成好的xml保存到test.xml文件中

2、Linq to XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    
XDocument document = new XDocument();
document.Declaration = new XDeclaration("1.0", "UTF-8", "");
         
XNamespace ns = "";
         
XElement root = new XElement(ns + "Z_AVS_UPLOAD_WEIGHT_Request",
    new XAttribute(XNamespace.Xmlns + "ns0", ""));
         
root.Add(new XElement("ZWERKS", "ZFM1"),
    new XElement("TAB1",
        new XElement("ZNO", 13022101),
        new XElement("ZORDER", 2013238955),
        new XElement("ZWEIGHT", 4140)),
    new XElement("TAB1",
        new XElement("ZNO", 13022101),
        new XElement("ZORDER", 2013238955),
        new XElement("ZWEIGHT", 4140))
        );
         
document.Add(root);
document.Save("test.xml");//保存xml到文件

可以发现Linq to XML的方法比较简洁,代码量也足够小。当然了XmlDocument的方法可以进一步的简化,这里重点展示一下Linq to XML的魅力。
阅读(1617) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~