新聞中心
本文向大家介紹LINQ To XML類,可能好多人還不了解LINQ To XML類,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

LINQ To XML類
下面重點學(xué)習三個類:XDocument,XElement,Xattribute
1.LINQ To XML類——XDocument類:表示一個 XML 文檔。XDocument可以包含以下元素:
◆一個 XDeclaration 對象。XDeclaration 使您能夠指定 XML 聲明的相關(guān)部分: XML 版本、文檔的編碼,以及 XML 文檔是否是獨立的。
◆一個 XElement 對象。 這是 XML 文檔的根節(jié)點。
◆任意數(shù)目的 XProcessingInstruction 對象。 處理指令將信息傳遞給處理 XML 的應(yīng)用程序。
◆任意數(shù)目的 XComment 對象。 注釋將與根元素同級。 XComment 對象不能是列表中的第一個參數(shù),因為 XML 文檔以注釋開頭無效。
◆一個用于 DTD 的 XDocumentType。
用XDocument創(chuàng)建XML文件
- XDocument d = new XDocument( new XDeclaration("1.0", "utf-8", "true"),
- new XComment("This is a comment."),
- new XProcessingInstruction("xml-stylesheet",
"href='mystyle.css' title='Compact' type='text/css'"),- new XElement("Pubs",
- new XElement("Book",
- new XElement("Title", "Artifacts of Roman Civilization"),
- new XElement("Author", "Moreno, Jordao")
- )
- )
- );
- Console.WriteLine(d.Declaration );
- Console.WriteLine(d);
- //XML文件
- version="1.0" encoding="utf-8" standalone="true"?>
- href='mystyle.css' title='Compact' type='text/css'?>
Artifacts of Roman Civilization Moreno, Jordao
2.LINQ To XML類——XElement類:表示一個 XML 元素。XDocument 可以包含以下元素:
◆Xelement
◆Xcomment
◆XprocessingInstruction
◆XText
用XElement創(chuàng)建XML文件
- XElement xml1 = new XElement("Root",
- new XElement("Node1", 1),
- new XElement("Node2", 2),
- new XElement("Node3", 3),
- new XElement("Node4", 4),
- new XElement("Node5", 5),
- new XElement("Node6", 6)
- );
- XElement xml2 = new XElement("Root",
- from el in xml1.Elements()
- where ((int)el >= 3 && (int)el <= 5)
- select el
- );
- Console.WriteLine(xml2);
- //XML文件
- <Root>
3 4 5
3.LINQ To XML類——XAttribute類:屬性是與元素關(guān)聯(lián)的名稱/值對。 XAttribute 類表示 XML 屬性。
屬性與元素之間有些區(qū)別。XAttribute 對象不是 XML 樹中的節(jié)點。 它們是與 XML 元素關(guān)聯(lián)的名稱/值對。 與文檔對象模型 (DOM) 相比,這更加貼切地反映了 XML 結(jié)構(gòu)。 雖然 XAttribute 對象實際上不是 XML 樹的節(jié)點,但使用 XAttribute 對象與使用 XElement 對象非常相似。
- XElement phone = new XElement("Phone",
- new XAttribute("Type", "Home"),
- "555-555-5555");
- Console.WriteLine(phone);
本文名稱:LINQToXML類詳細分析
URL標題:http://www.5511xx.com/article/djicicg.html


咨詢
建站咨詢
