XDocument 类    表示 XML 文档。
XElement 类   表示一个 XML 元素。

XDocument 类    表示 XML 文档。XElement 类   表示一个 XML 元素。

1, 创建元素及其属性并赋值。

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Collections;
6  using System.Xml.Linq;
7  /// <summary>
8  /// 继承层次结构
9  ///System.Object
10  /// System.Xml.Linq.XObject
11  /// System.Xml.Linq.XNode
12  /// System.Xml.Linq.XContainer
13  /// System.Xml.Linq.XElement
14  /// </summary>
15  
16  namespace element
17 {
18 class Program
19 {
20 static void Main(string[] args)
21 {
22 XElement root;
23
24 root = new XElement("Root", "Some text");
25 Console.WriteLine(root); // <Root>Some text</Root>
26  
27 root = new XElement("Root",
28 new XElement("NewChild", "n")
29 );
30 Console.WriteLine(root);// <Root> <NewChild>n</NewChild> </Root>
31  
32
33 root = new XElement("Root",
34 new XAttribute("NewAttribute", "n")
35 );
36 Console.WriteLine(root); //<Root NewAttribute="n" />
37  
38
39 double dbl = 12.345;
40 root = new XElement("Root", dbl);
41 Console.WriteLine(root); //<Root>12.345</Root>
42  
43 string[] stringArray = { "abc", "def", "ghi"};
44 root = new XElement("Root", stringArray);
45 Console.WriteLine(root); //<Root>abcdefghi</Root>
46  
47
48 XElement[] ellArray = {
49 new XElement("NewChild1", 1),
50 new XElement("NewChild2", 2),
51 new XElement("NewChild3", 3)
52 };
53 root = new XElement("Root", ellArray);
54 Console.WriteLine(root);
55
56 XAttribute[] attArray = {
57 new XAttribute("NewAtt1", 1),
58 new XAttribute("NewAtt2", 2),
59 new XAttribute("NewAtt3", 3)
60 };
61 root = new XElement("Root", attArray);
62 Console.WriteLine(root); //<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />
63  
64
65
66
67 XElement xmlTree1 = new XElement("Root",
68 new XElement("Child1", 1),
69 new XElement("Child2", 2),
70 new XElement("Child3", 3),
71 new XElement("Child4", 4),
72 new XElement("Child5", 5),
73 new XElement("Child6", 6)
74 );
75
76 XElement xmlTree2 = new XElement("Root",
77 from el in xmlTree1.Elements()
78 where ((int)el >= 3 && (int)el <= 5)
79 select el);
80
81 }
82 }
83 }
84

 

 

2,创建一个xml file

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml.Linq;
/// <summary>
/// 继承层次结构
///System.Object
/// System.Xml.Linq.XObject
/// System.Xml.Linq.XNode
/// System.Xml.Linq.XContainer
/// System.Xml.Linq.XDocument
/// </summary>
///

namespace element
{
class Program
{
static void Main(string[] args)
{
XDocument srcTree
= new XDocument(
new XComment("This is a comment"),
new XElement("Root",
new XElement("Child1", "data1"),
new XElement("Child2", "data2"),
new XElement("Child3", "data3"),
new XElement("Child2", "data4"),
new XElement("Info5", "info5"),
new XElement("Info6", "info6"),
new XElement("Info7", "info7"),
new XElement("Info8", "info8")
)
);

XDocument doc
= new XDocument(
new XComment("This is a comment"),
new XElement("Root",
from el
in srcTree.Element("Root").Elements()
where ((string)el).StartsWith("data")
select el
)
);
Console.WriteLine(doc);
}
}
}

 

该示例产生下面的输出:

 

<!--This is a comment-->
<Root>
<Child1>data1</Child1>
<Child2>data2</Child2>
<Child3>data3</Child3>
<Child2>data4</Child2>
</Root>

 

3,读取一个xml

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Xml.Linq;
6  using System.Collections;
7  namespace Indexer
8 {
9 class Program
10 {
11 private static XDocument _imgConfigXMLDoc;
12
13 const string imgConfigPath = @"D:\Jobs MDUG\Attachment\Template\XSD\ProcessControl\Excel\IMGAttribValues_SAP.xml";
14 static void Main(string[] args)
15 {
16
17 _imgConfigXMLDoc = XDocument.Load(imgConfigPath);//read xml file
18   Console.Write(_imgConfigXMLDoc);
19
20 var industryIMGList = _imgConfigXMLDoc.Elements("Industries");//return IEnumerable<T> 接口
21  
22 foreach (var industryIMG in industryIMGList)
23 {
24 /// <summary>
25 /// XContainer.Elements 方法
26 /// 返回值strIndustryImgValueList类型:System.Collections.Generic.IEnumerable<XElement>XElement 的 IEnumerable<T>,其中按文档顺序包含此 XContainer 的子元素。
27 /// </summary>
28   var strIndustryImgValueList = from attribValue in industryIMG.Elements("Industry").Elements("DropDown")
29 where
30 (string)attribValue.Element("ColumnId") == "Frequency"
31 select
32 attribValue.Element("ValueSet");
33 ArrayList MyArray1 = new ArrayList();
34
35 foreach (string s in strIndustryImgValueList.ElementAt(0).Value.ToString().Split(','))
36 {
37 MyArray1.Add(s);
38 }
39
40 }
41
42 Console.Read();
43 }
44 }
45 }
46
47  

 

 

4,XDocument.Parse 方法 (String):从字符串创建新 XDocument,XDocument.Load 方法 (String):从文件创建新 XDocument。


 

代码
1 //XDocument.Parse 方法 (String):从字符串创建新 XDocument。
2  
3  string str =
4  @"<?xml version=""1.0""?>
5 <!-- comment at the root level -->
6 <Root>
7 <Child>Content</Child>
8 </Root>";
9 XDocument doc = XDocument.Parse(str);
10 Console.WriteLine(doc);
11
12
13  //XDocument.Load 方法 (String):从文件创建新 XDocument。
14  XDocument doc = XDocument.Load("PurchaseOrder.xml");
15 Console.WriteLine(doc);
16
17  //转换:
18  
19
20 _imgConfigXMLDoc2 = XDocument.Load(imgConfigPath);
21
22 string s = _imgConfigXMLDoc2.ToString();
23
24 _imgConfigXMLDoc = XDocument.Parse(s);