linq to xml

34
1 LINQ TO XML Contents 1. Gii thiu LINQ to XML:............................................................................................. 3 2. Xây dng mt cây XML bng Visual C# 2008: ........................................................... 4 2.1. To mt phn tXML: .......................................................................................... 4 2.2. To mt thuc tính cho phn tXML: .................................................................. 5 2.3. To ghi chú trong cây XML: ................................................................................. 6 2.4. Xây dng mt cây XML: ....................................................................................... 7 2.5. Đối tượng XDeclaration:........................................................................................ 9 2.6. Phương thức XElement.Save: .............................................................................. 10 2.7. Phương thức XDocument.Save:........................................................................... 11 2.8. Phương thức XElement.Load: ............................................................................. 12 2.9. Phương thức XDocument.Load: .......................................................................... 13 3. XML namespace: ........................................................................................................ 14 3.1. Gii thiu namespace: .......................................................................................... 14 3.2. To mt namespace trong cây XML:................................................................... 15 3.3. Điều khin tin tnamespace trong cây XML: ................................................... 16 3.4. Viết truy vn LINQ trong namespace: ................................................................. 17 4. Nhng thao tác truy vấn cơ bản trên cây XML: ......................................................... 18 4.1. Tìm mt phn ttrong cây XML: ........................................................................ 18 4.2. Lc phn ttrong cây XML: ............................................................................... 19 4.3. Sp xếp các phn ttrong cây XML: .................................................................. 21

Upload: dangxthanh

Post on 22-Aug-2014

192 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: LINQ to XML

1

LINQ TO XML

Contents

1. Giới thiệu LINQ to XML: ............................................................................................. 3

2. Xây dựng một cây XML bằng Visual C# 2008: ........................................................... 4

2.1. Tạo một phần tử XML: .......................................................................................... 4

2.2. Tạo một thuộc tính cho phần tử XML: .................................................................. 5

2.3. Tạo ghi chú trong cây XML: ................................................................................. 6

2.4. Xây dựng một cây XML: ....................................................................................... 7

2.5. Đối tượng XDeclaration: ........................................................................................ 9

2.6. Phương thức XElement.Save: .............................................................................. 10

2.7. Phương thức XDocument.Save: ........................................................................... 11

2.8. Phương thức XElement.Load: ............................................................................. 12

2.9. Phương thức XDocument.Load: .......................................................................... 13

3. XML namespace: ........................................................................................................ 14

3.1. Giới thiệu namespace: .......................................................................................... 14

3.2. Tạo một namespace trong cây XML: ................................................................... 15

3.3. Điều khiển tiền tố namespace trong cây XML: ................................................... 16

3.4. Viết truy vấn LINQ trong namespace: ................................................................. 17

4. Những thao tác truy vấn cơ bản trên cây XML: ......................................................... 18

4.1. Tìm một phần tử trong cây XML: ........................................................................ 18

4.2. Lọc phần tử trong cây XML: ............................................................................... 19

4.3. Sắp xếp các phần tử trong cây XML: .................................................................. 21

Page 2: LINQ to XML

2

4.4. Kết hai cây XML: ................................................................................................ 22

4.5. Nhóm các phần tử trong một cây XML: .............................................................. 25

5. Những thao tác biến đổi trên cây XML: ..................................................................... 28

5.1. Thêm phần tử, thuộc tính và nút vào một cây XML:........................................... 28

5.2. Thay đổi phần tử, thuộc tính và nút của một cây XML: ...................................... 30

5.3. Xóa phần tử, thuộc tính và nút từ một cây XML: ................................................ 33

Page 3: LINQ to XML

3

1. Giới thiệu LINQ to XML:

LINQ to XML cung cấp một giao diện lập trình XML.

LINQ to XML sử dụng những ngôn ngữ mới nhất của .NET Language Framework và

được nâng cấp, thiết kế lại với giao diện lập trình XML Document Object Model (DOM).

XML đã được sử dụng rộng rãi để định dạng dữ liệu trong một loạt các ngữ cảnh (các

trang web, trong các tập tin cấu hình, trong các tập tin Microsoft Office Word, và trong

cơ sở dữ liệu).

LINQ to XML có cấu trúc truy vấn tương tự SQL. Nhà phát triển trung bình có thể viết

các truy vấn ngắn gọn, mạnh mẽ, viết mã ít hơn nhưng có ý nghĩa nhiều hơn. Họ có thể

sử dụng các biểu thức truy vấn từ nhiều dữ liệu các lĩnh vực tại một thời điểm.

LINQ to XML cũng giống như Document Object Model (DOM) ở chỗ nó chuyển các tài

liệu XML vào bộ nhớ. Bạn có thể truy vấn và sửa đổi các tài liệu, và sau khi bạn chỉnh

sửa nó, bạn có thể lưu nó vào một tập tin hoặc xuất nó ra. Tuy nhiên, LINQ to XML khác

DOM: Nó cung cấp mô hình đối tượng mới đơn giản hơn và dễ thao tác hơn để làm việc ,

và đó là tận dụng các cải tiến ngôn ngữ trong Visual C # 2008.

Khả năng sử dụng kết quả truy vấn là tham số cho đối tượng XElement và XAttribute cho

phép một phương pháp mạnh mẽ để tạo ra cây XML. Phương pháp này, được gọi là

functional construction, cho phép các nhà phát triển để dễ dàng chuyển đổi cây XML từ một

hình dạng này sang hình dạng khác.

Sử dụng LINQ to XML bạn có thể:

Load XML từ nhiều file hoặc luồng.

Xuất XML ra file hoặc luồng.

Truy vấn cây XML bằng những truy vấn LINQ.

Thao tác cây XML trong bộ nhớ.

Biến đổi cây XML từ dạng này sang dạng khác.

Page 4: LINQ to XML

4

2. Xây dựng một cây XML bằng Visual C# 2008:

2.1. Tạo một phần tử XML:

Cấu trúc

XElement(XName name, object content)

XElement(XName name)

XName: tên phần tử.

object: nội dụng của phần tử.

Ví dụ sau tạo phần tử <Customer>có nội dung “Adventure Works”.

C#

XElement n = new XElement("Customer", "Adventure

Works");

Console.WriteLine(n);

Xml

<Customer>Adventure Works</Customer>

Tạo phần tử rỗng để trống phân nội dung:

C#

XElement n = new XElement("Customer");

Console.WriteLine(n);

Xml

<Customer />

Page 5: LINQ to XML

5

2.2. Tạo một thuộc tính cho phần tử XML:

Cấu trúc

XAttribute(XName name, object content)

Tham số:

XName: thuộc tính.

object: giá trị của thuộc tính.

C#

XElement phone = new XElement("Phone",

new XAttribute("Type", "Home"),

"555-555-5555");

Console.WriteLine(phone);

Xml

<Phone Type="Home">555-555-5555</Phone>

Page 6: LINQ to XML

6

2.3. Tạo ghi chú trong cây XML:

Tạo một phần tử gồm một chú thích như một nút con

Tên

XComment(String)

C#

XElement root = new XElement("Root",

new XComment("This is a comment")

);

Console.WriteLine(root);

Xml

<Root>

<!--This is a comment-->

</Root>

Page 7: LINQ to XML

7

2.4. Xây dựng một cây XML:

Sử dụng đối tượng XDocument xây dựng một cây XML. Thông thường ta có thể tạo ra

cây XML với nút gốc XElement.

Name Description

XDocument() Initializes a new instance of the

XDocument class.

XDocument(Object[]) Initializes a new instance of the

XDocument class with the specified

content.

XDocument(XDocument) Initializes a new instance of the

XDocument class from an existing

XDocument object.

XDocument(XDeclaration,

Object[])

Initializes a new instance of the

XDocument class with the specified

XDeclaration and content.

C#

XDocument doc = new XDocument(

new XComment("This is a comment"),

new XElement("Root",

new XElement("Info5", "info5"),

new XElement("Info6", "info6"),

new XElement("Info7", "info7"),

new XElement("Info8", "info8")

)

);

Console.WriteLine(doc);

Xml

<!--This is a comment-->

Page 8: LINQ to XML

8

<Root>

<Child1>data1</Child1>

<Child2>data2</Child2>

<Child3>data3</Child3>

<Child2>data4</Child2>

</Root>

Page 9: LINQ to XML

9

2.5. Đối tượng XDeclaration:

Đối tượng XDeclaration được sử dụng để khai báo XML version, encoding, and có thể có

hoặc không thuộc tính standalone của một tài liệu XML.

Cấu trúc

XDeclaration(string version,string encoding,string standalone)

C#

XDocument doc = new XDocument(

new XDeclaration("1.0", "utf-8", "yes"),

new XComment("This is a comment"),

new XElement("Root", "content")

);

doc.Save("Root.xml");

Console.WriteLine(File.ReadAllText("Root.xml"));

Xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<!--This is a comment-->

<Root>content</Root>

Page 10: LINQ to XML

10

2.6. Phương thức XElement.Save:

Name Description

Save(String) Serialize this element to a file.

C#

XElement root = new XElement("Root",

new XElement("Child", "child content")

);

root.Save("Root.xml");

string str = File.ReadAllText("Root.xml");

Console.WriteLine(str);

Xml

<?xml version="1.0" encoding="utf-8"?>

<Root>

<Child>child content</Child>

</Root>

Page 11: LINQ to XML

11

2.7. Phương thức XDocument.Save:

Name Description

Save(String) Serialize this XDocument to a file.

C#

XDocument doc = new XDocument(

new XElement("Root",

new XElement("Child", "content")

)

);

doc.Save("Root.xml");

Console.WriteLine(File.ReadAllText("Root.xml"));

Xml

<?xml version="1.0" encoding="utf-8"?>

<Root>

<Child>content</Child>

</Root>

Page 12: LINQ to XML

12

2.8. Phương thức XElement.Load:

Name Description

Load(String) Loads an XElement from a file.

C#

XElement xmlTree1 = new XElement("Root",

new XElement("Child", "content")

);

xmlTree1.Save("Tree.xml");

XElement xmlTree2 = XElement.Load("Tree.xml");

Console.WriteLine(xmlTree2);

<Root>

<Child>content</Child>

</Root>

Page 13: LINQ to XML

13

2.9. Phương thức XDocument.Load:

Name Description

Load(String) Creates a new XDocument from a file.

C#

XElement xmlTree1 = new XElement("Root",

new XElement("Child", "content")

);

xmlTree1.Save("Tree.xml");

XDocument xmlTree2 = XDocument.Load("Tree.xml");

Console.WriteLine(xmlTree2);

Xml

<Root>

<Child>content</Child>

</Root>

Page 14: LINQ to XML

14

3. XML namespace:

3.1. Giới thiệu namespace:

XML namespace giúp tránh xung đột giửa các bộ phận khác nhau của một tài liệu XML.

khi khai báo một namespace, bạn chọn một tên cục bộ sao cho nó là duy nhất.

Những tiền tố làm cho tài liệu XML súc tích và dễ hiểu hơn.

Một trong những lợi thế khi sử dụng LINQ to XML với C# là đơn giản hóa những XML

name là loại bỏ những thủ tục mà những nhà phát triễn sử dụng tiền tố. khi LINQ load

hoặc parse một tài liệu XML, mỗi tiền tố sẽ được xử lý để phù hợp với namespace XML.

khi làm việc với tài liệu có namespace, bạn thường truy cập namespace thông qua

namespace URI, không thông qua tiền tố namespace.

Page 15: LINQ to XML

15

3.2. Tạo một namespace trong cây XML:

Xem ví dụ sau:

C# Copy Code

// Create an XML tree in a namespace, with a specified

prefix

XNamespace aw = "http://www.adventure-works.com";

XElement root = new XElement(aw + "Root",

new XAttribute(XNamespace.Xmlns + "aw",

"http://www.adventure-works.com"),

new XElement(aw + "Child", "child content")

);

Console.WriteLine(root);

Xml

<aw:Root xmlns:aw="http://www.adventure-works.com">

<aw:Child>child content</aw:Child>

</aw:Root>

Page 16: LINQ to XML

16

3.3. Điều khiển tiền tố namespace trong cây XML:

Ví dụ sau khai báo hai tiền tố namespace. Tên miền http://www.adventure-works.com có

tiền tố aw, và www.fourthcoffee.com có tiền tố fc.

C#

XNamespace aw = "http://www.adventure-works.com";

XNamespace fc = "www.fourthcoffee.com";

XElement root = new XElement(aw + "Root",

new XAttribute(XNamespace.Xmlns + "aw",

"http://www.adventure-works.com"),

new XAttribute(XNamespace.Xmlns + "fc",

"www.fourthcoffee.com"),

new XElement(fc + "Child",

new XElement(aw + "DifferentChild", "other

content")

),

new XElement(aw + "Child2", "c2 content"),

new XElement(fc + "Child3", "c3 content")

);

Console.WriteLine(root);

Xml

<aw:Root xmlns:aw="http://www.adventure-works.com"

xmlns:fc="www.fourthcoffee.com">

<fc:Child>

<aw:DifferentChild>other

content</aw:DifferentChild>

</fc:Child>

<aw:Child2>c2 content</aw:Child2>

<fc:Child3>c3 content</fc:Child3>

</aw:Root>

Page 17: LINQ to XML

17

3.4. Viết truy vấn LINQ trong namespace:

Xem ví dụ sau:

C#

XNamespace aw = "http://www.adventure-works.com";

XElement root = XElement.Parse(

@"<Root xmlns='http://www.adventure-works.com'>

<Child>1</Child>

<Child>2</Child>

<Child>3</Child>

<AnotherChild>4</AnotherChild>

<AnotherChild>5</AnotherChild>

<AnotherChild>6</AnotherChild>

</Root>");

IEnumerable<XElement> c1 =

from el in root.Elements(aw + "Child")

select el;

foreach (XElement el in c1)

Console.WriteLine((int)el);

1

2

3

Page 18: LINQ to XML

18

4. Những thao tác truy vấn cơ bản trên cây XML:

4.1. Tìm một phần tử trong cây XML:

Xét ví dụ sau tìm phần tử Address có thuộc tính Type có giá trị là "Billing".

C#

XElement root = XElement.Load("PurchaseOrder.xml");

IEnumerable<XElement> address =

from el in root.Elements("Address")

where (string)el.Attribute("Type") == "Billing"

select el;

foreach (XElement el in address)

Console.WriteLine(el);

Xml

<Address Type="Billing">

<Name>Tai Yee</Name>

<Street>8 Oak Avenue</Street>

<City>Old Town</City>

<State>PA</State>

<Zip>95819</Zip>

<Country>USA</Country>

</Address>

Page 19: LINQ to XML

19

4.2. Lọc phần tử trong cây XML:

Ví dụ sau lọc những phần tử có phần tử con <Type > có Value="Yes".

C#

XElement root = XElement.Parse(@"<Root>

<Child1>

<Text>Child One Text</Text>

<Type Value=""Yes""/>

</Child1>

<Child2>

<Text>Child Two Text</Text>

<Type Value=""Yes""/>

</Child2>

<Child3>

<Text>Child Three Text</Text>

<Type Value=""No""/>

</Child3>

<Child4>

<Text>Child Four Text</Text>

<Type Value=""Yes""/>

</Child4>

<Child5>

<Text>Child Five Text</Text>

</Child5>

</Root>");

var cList =

from typeElement in

root.Elements().Elements("Type")

where (string)typeElement.Attribute("Value") ==

"Yes"

select (string)typeElement.Parent.Element("Text");

foreach(string str in cList)

Console.WriteLine(str);

Child One Text

Child Two Text

Page 20: LINQ to XML

20

Child Four Text

Page 21: LINQ to XML

21

4.3. Sắp xếp các phần tử trong cây XML:

C#

XElement root = XElement.Load("Data.xml");

IEnumerable<decimal> prices =

from el in root.Elements("Data")

let price = (decimal)el.Element("Price")

orderby price

select price;

foreach (decimal el in prices)

Console.WriteLine(el);

0.99

4.95

6.99

24.50

29.00

66.00

89.99

Page 22: LINQ to XML

22

4.4. Kết hai cây XML:

Ví dụ sau kết những phần tử Customer với những phần tử Order , và tạo ra tài liệu XML

mới gồm phần tử CompanyName bên trong order.

C#

XmlSchemaSet schemas = new XmlSchemaSet();

schemas.Add("", "CustomersOrders.xsd");

Console.Write("Attempting to validate, ");

XDocument custOrdDoc =

XDocument.Load("CustomersOrders.xml");

bool errors = false;

custOrdDoc.Validate(schemas, (o, e) =>

{

Console.WriteLine("{0}",

e.Message);

errors = true;

});

Console.WriteLine("custOrdDoc {0}", errors ? "did not

validate" : "validated");

if (!errors)

{

// Join customers and orders, and create a new XML

document with

// a different shape.

// The new document contains orders only for

customers with a

// CustomerID > 'K'

XElement custOrd = custOrdDoc.Element("Root");

XElement newCustOrd = new XElement("Root",

from c in

custOrd.Element("Customers").Elements("Customer")

join o in

custOrd.Element("Orders").Elements("Order")

Page 23: LINQ to XML

23

on (string)c.Attribute("CustomerID")

equals

(string)o.Element("CustomerID")

where

((string)c.Attribute("CustomerID")).CompareTo("K") > 0

select new XElement("Order",

new XElement("CustomerID",

(string)c.Attribute("CustomerID")),

new XElement("CompanyName",

(string)c.Element("CompanyName")),

new XElement("ContactName",

(string)c.Element("ContactName")),

new XElement("EmployeeID",

(string)o.Element("EmployeeID")),

new XElement("OrderDate",

(DateTime)o.Element("OrderDate"))

)

);

Console.WriteLine(newCustOrd);

}

Attempting to validate, custOrdDoc validated

<Root>

<Order>

<CustomerID>LAZYK</CustomerID>

<CompanyName>Lazy K Kountry Store</CompanyName>

<ContactName>John Steel</ContactName>

<EmployeeID>1</EmployeeID>

<OrderDate>1997-03-21T00:00:00</OrderDate>

</Order>

<Order>

<CustomerID>LAZYK</CustomerID>

<CompanyName>Lazy K Kountry Store</CompanyName>

<ContactName>John Steel</ContactName>

<EmployeeID>8</EmployeeID>

<OrderDate>1997-05-22T00:00:00</OrderDate>

</Order>

<Order>

<CustomerID>LETSS</CustomerID>

Page 24: LINQ to XML

24

<CompanyName>Let's Stop N Shop</CompanyName>

<ContactName>Jaime Yorres</ContactName>

<EmployeeID>1</EmployeeID>

<OrderDate>1997-06-25T00:00:00</OrderDate>

</Order>

<Order>

<CustomerID>LETSS</CustomerID>

<CompanyName>Let's Stop N Shop</CompanyName>

<ContactName>Jaime Yorres</ContactName>

<EmployeeID>8</EmployeeID>

<OrderDate>1997-10-27T00:00:00</OrderDate>

</Order>

<Order>

<CustomerID>LETSS</CustomerID>

<CompanyName>Let's Stop N Shop</CompanyName>

<ContactName>Jaime Yorres</ContactName>

<EmployeeID>6</EmployeeID>

<OrderDate>1997-11-10T00:00:00</OrderDate>

</Order>

<Order>

<CustomerID>LETSS</CustomerID>

<CompanyName>Let's Stop N Shop</CompanyName>

<ContactName>Jaime Yorres</ContactName>

<EmployeeID>4</EmployeeID>

<OrderDate>1998-02-12T00:00:00</OrderDate>

</Order>

</Root>

Page 25: LINQ to XML

25

4.5. Nhóm các phần tử trong một cây XML:

Ví dụ nhóm dữ liệu theo loại, sau đó tạo ra một tập tin XML mới, trong đó phân cấp

XML theo nhóm.

C#

XElement doc = XElement.Load("Data.xml");

var newData =

new XElement("Root",

from data in doc.Elements("Data")

group data by (string)data.Element("Category")

into groupedData

select new XElement("Group",

new XAttribute("ID", groupedData.Key),

from g in groupedData

select new XElement("Data",

g.Element("Quantity"),

g.Element("Price")

)

)

);

Console.WriteLine(newData);

Xml

<Root>

Page 26: LINQ to XML

26

<Group ID="A">

<Data>

<Quantity>3</Quantity>

<Price>24.50</Price>

</Data>

<Data>

<Quantity>5</Quantity>

<Price>4.95</Price>

</Data>

<Data>

<Quantity>3</Quantity>

<Price>66.00</Price>

</Data>

<Data>

<Quantity>15</Quantity>

<Price>29.00</Price>

</Data>

</Group>

<Group ID="B">

<Data>

<Quantity>1</Quantity>

<Price>89.99</Price>

Page 27: LINQ to XML

27

</Data>

<Data>

<Quantity>10</Quantity>

<Price>.99</Price>

</Data>

<Data>

<Quantity>8</Quantity>

<Price>6.99</Price>

</Data>

</Group>

</Root>

Page 28: LINQ to XML

28

5. Những thao tác biến đổi trên cây XML:

5.1. Thêm phần tử, thuộc tính và nút vào một cây XML:

Thêm nút vào cuối cây hoặc đầu cây dùng phương thức .Add và .AddFirst .

C#

XElement xmlTree = new XElement("Root",

new XElement("Child1", 1),

new XElement("Child2", 2),

new XElement("Child3", 3),

new XElement("Child4", 4),

new XElement("Child5", 5)

);

xmlTree.Add(new XElement("NewChild", "new content"));

Console.WriteLine(xmlTree);

<Root>

<Child1>1</Child1>

<Child2>2</Child2>

<Child3>3</Child3>

<Child4>4</Child4>

<Child5>5</Child5>

<NewChild>new content</NewChild>

Page 29: LINQ to XML

29

</Root>

Page 30: LINQ to XML

30

5.2. Thay đổi phần tử, thuộc tính và nút của một cây XML:

Sử dụng phương thức XAttribute.SetValue thay đổi giá trị thuộc tính "Att" từ"root"

thành"new content".

C#

XElement root = new XElement("Root",

new XAttribute("Att", "root"),

(“Root”)

);

Console.WriteLine(root);

Console.WriteLine(“---------------”)

XAttribute att = root.Attribute("Att");

att.SetValue("new content");

root.SetValue("new content");

Console.WriteLine(root);

<Root Att="root">Root</Root>

<-------------->

<Root Att="new content">new content</Root>

Sử dụng phương thức XNode.ReplaceWith thay đổi nút có tên "Child3"có nội dung

"child3 content" thành "NewChild" nội dung "new content" .

C#

XElement xmlTree = new XElement("Root",

new XElement("Child1", "child1 content"),

new XElement("Child2", "child2 content"),

new XElement("Child3", "child3 content"),

new XElement("Child4", "child4 content"),

new XElement("Child5", "child5 content")

);

XElement child3 = xmlTree.Element("Child3");

child3.ReplaceWith(

new XElement("NewChild", "new content")

);

Page 31: LINQ to XML

31

Console.WriteLine(xmlTree);

Xml

<Root>

<Child1>child1 content</Child1>

<Child2>child2 content</Child2>

<NewChild>new content</NewChild>

<Child4>child4 content</Child4>

<Child5>child5 content</Child5>

</Root>

Thiết lập giá trị của lớp con bằng hàm XElement.SetElementValue.

C#

// Create an element with no content

XElement root = new XElement("Root");

// Add some name/value pairs.

root.SetElementValue("Ele1", 1);

root.SetElementValue("Ele2", 2);

root.SetElementValue("Ele3", 3);

Console.WriteLine(root);

// Modify one of the name/value pairs.

root.SetElementValue("Ele2", 22);

Console.WriteLine(root);

// Remove one of the name/value pairs.

root.SetElementValue("Ele3", null);

Console.WriteLine(root);

<Root>

<Ele1>1</Ele1>

<Ele2>2</Ele2>

<Ele3>3</Ele3>

</Root>

<Root>

Page 32: LINQ to XML

32

<Ele1>1</Ele1>

<Ele2>22</Ele2>

<Ele3>3</Ele3>

</Root>

<Root>

<Ele1>1</Ele1>

<Ele2>22</Ele2>

</Root>

Page 33: LINQ to XML

33

5.3. Xóa phần tử, thuộc tính và nút từ một cây XML:

Ví dụ sau dùng phương thức XElement.RemoveAll xóa những phần tử con và thuộc tính của một

cây XML

C#

XElement root = new XElement("Root",

new XAttribute("Att1", 1),

new XAttribute("Att2", 2),

new XAttribute("Att3", 3),

new XElement("Child1", 1),

new XElement("Child2", 2),

new XElement("Child3", 3)

);

root.RemoveAll(); // removes children elements and

attributes of root

Console.WriteLine(root);

Xml

<Root />

Ví dụ tiếp theo dùng phương thức XElement.RemoveAttributes xóa toàn bộ thuộc tính của một

cây XML.

C#

XElement root = new XElement("Root",

new XAttribute("Att1", 1),

new XAttribute("Att2", 2),

new XAttribute("Att3", 3),

new XElement("Child1", 1),

new XElement("Child2", 2),

new XElement("Child3", 3)

);

root.RemoveAttributes();

Console.WriteLine(root);

Xml

Page 34: LINQ to XML

34

<Root>

<Child1>1</Child1>

<Child2>2</Child2>

<Child3>3</Child3>

</Root>

Dùng phương thức XNode.Remove xóa một nút của cây XML

C#

XElement xmlTree = new XElement("Root",

new XElement("Child1", "child1 content"),

new XElement("Child2", "child2 content"),

new XElement("Child3", "child3 content"),

new XElement("Child4", "child4 content"),

new XElement("Child5", "child5 content")

);

XElement child3 = xmlTree.Element("Child3");

child3.Remove();

Console.WriteLine(xmlTree);

Xml

<Root>

<Child1>child1 content</Child1>

<Child2>child2 content</Child2>

<Child4>child4 content</Child4>

<Child5>child5 content</Child5>

</Root>