Tải bản đầy đủ (.pptx) (12 trang)

Chapter 5 - LINQ to XML

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (209.89 KB, 12 trang )

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ via C# 3.0
Chapter 5 – LINQ to XML
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to XML

Introducing a light-weight XML object
model: XElement

Generating XML fragments with the
XElement API

Querying XElement DOMs
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Primary LINQ to XML Types

LINQ to XML is implemented in
System.Xml.Linq.dll

Primary classes:

XElement – can contain a document or a fragment

XDocument – full-fledged XML document (with
declaration, processing instructions etc.)

XAttribute – represents an XML attribute

In-memory XmlReader-based model
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Constructing XML



Construction statements closely mirror the
XML structure:
XElement library =
new XElement("library",
new XElement("book",
new XAttribute("id", "1234"),
new XElement("author", "Juwal Lowy"),
new XElement("title", "Programming WCF")),
new XElement("book",
new XAttribute("id", "5678"),
new XElement("author", "Jeffrey Richter"),
new XElement("title", "CLR via C# 2.0")));
Console.WriteLine(library);
<library>
<book id="1234">
<author>Juwal Lowy</author>
<title>Programming WCF</title>
</book>
<book id="5678">
<author>Jeffrey Richter</author>
<title>CLR via C# 2.0</title>
</book>
</library>
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Constructing XML from a Query

Various LINQ sources working together:
XElement catalog =
new XElement("students",

(from student in students
select new XElement("student",
new XAttribute("name", student.Name),
new XAttribute("id", student.Id),
new XElement("grades",
from grade in student.Grades
select new XElement("grade", grade)
)
)
).Take(3));
<students>
<student name="John" id="1">
<grades>
<grade>78</grade>
<grade>89</grade>
<grade>84</grade>
</grades>
</student>
<student ... />
</students>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×