国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

(十一)CSharp-LINQ-LINQToXML(4)

這篇具有很好參考價(jià)值的文章主要介紹了(十一)CSharp-LINQ-LINQToXML(4)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、XML 類

1、創(chuàng)建、保存、加載和顯示 XML 文檔

        static void Main(string[] args)
        {
            XDocument employees1 =
                new XDocument(//創(chuàng)建 XML 文檔
                    new XElement("Employees",//創(chuàng)建根元素
                    new XElement("Name", "Box Smith"),//創(chuàng)建元素
                    new XElement("Name", "Sally Jones")//創(chuàng)建元素
                    )
                    );

            employees1.Save("EmployeesFile.xml");//保存到文件

            //將保存的文檔加載到新變量中
            XDocument employees2 = XDocument.Load("EmployeesFile.xml");

            Console.WriteLine(employees2);//顯示文檔
            Console.ReadKey();
        }

輸出結(jié)果:

><Employees>
  <Name>Box Smith</Name>
  <Name>Sally Jones</Name>
</Employees>
1)創(chuàng)建 XML 樹
        static void Main(string[] args)
        {
            XDocument employeeDoc =
                new XDocument(//創(chuàng)建文檔
                    new XElement("Employees",//創(chuàng)建根元素

                    new XElement("Employee",//第一個(gè) Employee 元素
                    new XElement("Name", "Box Smith"),
                    new XElement("PhoneNumber", "408-555-10000")),

                    new XElement("Employee",//第二個(gè) Employee 元素
                    new XElement("Name", "Sally Jones"),
                    new XElement("PhoneNumber", "415-555-2000"),
                     new XElement("PhoneNumber", "415-555-2001"))

                    )
                    );

            Console.WriteLine(employeeDoc);//顯示文檔
            Console.ReadKey();
        }

輸出結(jié)果:

<Employees>
  <Employee>
    <Name>Box Smith</Name>
    <PhoneNumber>408-555-10000</PhoneNumber>
  </Employee>
  <Employee>
    <Name>Sally Jones</Name>
    <PhoneNumber>415-555-2000</PhoneNumber>
    <PhoneNumber>415-555-2001</PhoneNumber>
  </Employee>
</Employees>
2)使用 XML 樹的值

表20-2 查詢 XML 的方法

方法名稱 返回類型 描述
Nodes Xdocument,XElement IEnumerable<object> 返回當(dāng)前節(jié)點(diǎn)的所有節(jié)點(diǎn)(不管是什么類型)
Elements Xdocument,XElement IEnumerable<XElement> 返回當(dāng)前節(jié)點(diǎn)的 XElement 子節(jié)點(diǎn),或所有具有某個(gè)名字的子節(jié)點(diǎn)
Element Xdocument,XElement XElement 返回當(dāng)前節(jié)點(diǎn)的第一個(gè) XElement 子節(jié)點(diǎn),或具有某個(gè)名字的子節(jié)點(diǎn)
Descendants XElement IEnumerable<XElement> 返回所有的 XElement 子代節(jié)點(diǎn),或所有具有某個(gè)名字的 XElement 子代節(jié)點(diǎn),不管它們處于當(dāng)前節(jié)點(diǎn)下什么嵌套級別
DescendantsAndSelf XElement IEnumerable<XElement> 和 Descendants 一樣,但是包括當(dāng)前節(jié)點(diǎn)
Ancestors XElement IEnumerable<XElement> 返回所有上級 XElement 節(jié)點(diǎn),或者所有具有某個(gè)名字的上級 XElement 節(jié)點(diǎn)
AncestorsAndSelf XElement IEnumerable<XElement> 和 Ancestors 一樣,但是包括當(dāng)前節(jié)點(diǎn)
Parent XElement XElement 返回當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)
  • Nodes
//獲取 XComment 節(jié)點(diǎn)
IEnumerable<XComment> comments = xd.Nodes().OfType<XComment>();
  • Elements
//獲取名為 PhoneNumer 的子 XElement 節(jié)點(diǎn)
IEnumerable<XElement> empPhones = emp.Element("PhoneNumer");
  • Element
    獲取第一個(gè)子 XElement 節(jié)點(diǎn)。

Element 和 Elements 方法示例:

       static void Main(string[] args)
        {
            XDocument employeeDoc =
                new XDocument(
                    new XElement("Employees",

                    new XElement("Employee",
                    new XElement("Name", "Box Smith"),
                    new XElement("PhoneNumber", "408-555-10000")),

                    new XElement("Employee",
                    new XElement("Name", "Sally Jones"),
                    new XElement("PhoneNumber", "415-555-2000"),
                     new XElement("PhoneNumber", "415-555-2001"))

                    )
                    );

            XElement root = employeeDoc.Element("Employees");
            IEnumerable<XElement> employees = root.Elements();

            foreach(XElement emp in employees)
            {
                XElement empNameNode = emp.Element("Name");
                Console.WriteLine(empNameNode.Value);

                IEnumerable<XElement> empPhones = emp.Elements("PhoneNumber");
                foreach (XElement phone in empPhones)
                    Console.WriteLine($"     { phone.Value }");
            }

            Console.ReadKey();
        }

輸出結(jié)果:

Box Smith
408-555-10000
Sally Jones
415-555-2000
415-555-2001

3)增加節(jié)點(diǎn)以及操作 XML
   static void Main(string[] args)
        {
            XDocument xd = new XDocument(//創(chuàng)建 XML 樹
                new XElement("root",
                new XElement("first")
                )
                );

            Console.WriteLine("Original tree");
            Console.WriteLine(xd);Console.WriteLine();//顯示樹

            XElement rt = xd.Element("root");//獲取第一個(gè)元素
            rt.Add(new XElement("second"));//添加子元素
            rt.Add(new XElement("third"),//再添加3個(gè)子元素
                new XComment("Important Comment"),
                new XElement("fourth"));

            Console.WriteLine("Modified tree");
            Console.WriteLine(xd);//顯示 Modified tree

            Console.ReadKey();
        }

輸出結(jié)果:

Original tree
<root>
  <first />
</root>

Modified tree
<root>
  <first />
  <second />
  <third />
  <!--Important Comment-->
  <fourth />
</root>

表20-3 操作 XML 的方法

方法名稱 從哪里調(diào)用 描述
Add 父節(jié)點(diǎn) 在當(dāng)前節(jié)點(diǎn)的既有子節(jié)點(diǎn)前后加新的子節(jié)點(diǎn)
AddFirst 父節(jié)點(diǎn) 在當(dāng)前節(jié)點(diǎn)的既有子節(jié)點(diǎn)前增加新的子節(jié)點(diǎn)
AddBeforeSelf 節(jié)點(diǎn) 在同級別的當(dāng)前節(jié)點(diǎn)之前增加新的節(jié)點(diǎn)
AddAfterSelf 節(jié)點(diǎn) 在同級別的當(dāng)前節(jié)點(diǎn)之后增加新的節(jié)點(diǎn)
Remove 節(jié)點(diǎn) 刪除當(dāng)前所選的節(jié)點(diǎn)及其內(nèi)容
RemoveNodes 節(jié)點(diǎn) 刪除當(dāng)前所選的 XElement 及其內(nèi)容
SetElement 父節(jié)點(diǎn) 設(shè)置節(jié)點(diǎn)的內(nèi)容

2、使用 XML 特性

特性提供了有關(guān) XElement 節(jié)點(diǎn)的額外信息,它放在 XML 元素的開始標(biāo)簽中。
如:

        static void Main(string[] args)
        {
            XDocument xd = new XDocument(
                new XElement("root",
                new XAttribute("color", "red"),//特性構(gòu)造函數(shù)
                new XAttribute("Size", "large"),//特性構(gòu)造函數(shù)
                new XElement("first"),
                new XElement("second")
                )
                );

            Console.WriteLine(xd);

            XElement rt = xd.Element("root");//獲取元素

            XAttribute color = rt.Attribute("color");
            XAttribute size = rt.Attribute("Size");//獲取特性

            Console.WriteLine($"color is {color.Value}");//顯示特性值
            Console.WriteLine($"size is {size.Value}");

//移除特性
//rt.Attribute("color").Remove();//移除 color 特性
//rt.SetAttributeValue("Size",nulll);//移除 size 特性

//變更特性值
//rt.SetAttributeValue("Size","medium");
//rt.SetAttributeValue("width","narrow");//如果沒有這個(gè)特性,就添加特性
            Console.ReadKey();
        }

輸出結(jié)果:

<root color="red" Size="large">
  <first />
  <second />
</root>
color is red
size is large

2、其他類型的節(jié)點(diǎn)

1)XComment
new XComment("This is a comment");

XML 文檔行為:

<!--This is a comment-->
2)XDeclaration

XML 文檔從包含 XML 使用的版本號、使用的字符編碼類型以及文檔是否依賴外部引用的一行開始。

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

XML 文檔行:

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

XML 處理指令用于提供關(guān)于 XML 文檔的使用和解釋方法的額外數(shù)據(jù)。處理指令最常用于關(guān)聯(lián) XML 文檔和樣式表。

可以使用 XProcessingInstruction 構(gòu)造函數(shù)來包含處理指令。它接受兩個(gè)字符串參數(shù):目標(biāo)和數(shù)據(jù)串。如果處理指令接受多個(gè)數(shù)據(jù)參數(shù),這些參數(shù)必須包含在 XProcessingInstruction 構(gòu)造函數(shù)的第二個(gè)字符串參數(shù)中。

new XProcessingInstruction("XML-stypesheet",
						    @"href=""stories"", type=""text/css""")

XML 文檔行:

<?xml-stylesheet href="stories.css" type=""text/css?>

例如:

     static void Main(string[] args)
        {
            XDocument xd = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("This is a comment"),
                new XProcessingInstruction("xml-stylesheet",
                                            @"href=""stories.css"" type=""text/css"""),
                new XElement("root",
                new XElement("first"),
                new XElement("second")
                )
                );

            Console.WriteLine(xd);

            Console.ReadKey();
        }

輸出結(jié)果:

<!--This is a comment-->
<?xml-stylesheet href="stories.css" type="text/css"?>
<root>
  <first />
  <second />
</root>

二、使用 LINQ to XML 的 LINQ 查詢

1)創(chuàng)建保存 XML 文檔:

示例:

        static void Main(string[] args)
        {
            XDocument xd = new XDocument(
                new XElement("MyElements",
                new XElement("first",
                new XAttribute("color","red"),
                new XAttribute("size","small")),

                 new XElement("second",
                new XAttribute("color", "red"),
                new XAttribute("size", "medium")),

                 new XElement("third",
                new XAttribute("color", "blue"),
                new XAttribute("size", "large"))));


            Console.WriteLine(xd);//顯示 XML 樹
            xd.Save("SimpleSample.xml");//保存 XML 樹
            Console.ReadKey();
        }

XML 文檔行:

<MyElements>
  <first color="red" size="small" />
  <second color="red" size="medium" />
  <third color="blue" size="large" />
</MyElements>

2)加載 XML 文檔,使用 LINQ 查詢 XML:

        static void Main(string[] args)
        {
            XDocument xd =  XDocument.Load("SimpleSample.xml"); //加載文檔
            XElement rt = xd.Element("MyElements"); //獲取根元素

            var xyz = from e in rt.Elements() //選擇名稱包含
                      where e.Name.ToString().Length == 5 //5個(gè)字符的元素
                      select e;

            foreach (XElement x in xyz)
                Console.WriteLine(x.Name.ToString());//顯示所選的元素

            Console.WriteLine();

            foreach (XElement x in xyz)
                Console.WriteLine("Name:{0}, color: {1},size: {2}",
                                  x.Name,
                                  x.Attribute("color").Value,
                                  x.Attribute("size").Value);

            Console.ReadKey();
        }

輸出結(jié)果:

first
third

Name:first, color: red,size: small
Name:third, color: blue,size: large

3)如下代碼使用了一個(gè)簡單的查詢來獲取 XML 樹的所有頂層元素,并且為每一個(gè)元素創(chuàng)建了一個(gè)匿名類型的對象。

        static void Main(string[] args)
        {
            XDocument xd =  XDocument.Load("SimpleSample.xml"); //加載文檔
            XElement rt = xd.Element("MyElements"); //獲取根元素

            var xyz = from e in rt.Elements()
                      select new { e.Name, color = e.Attribute("color") };

            foreach (var x in xyz)
                Console.WriteLine(x);//默認(rèn)格式化

            Console.WriteLine();

            foreach (var x in xyz)
                Console.WriteLine("{0,-6},    color: {1,-7}",x.Name,x.color.Value);

            Console.WriteLine();

            Console.ReadKey();
        }

//創(chuàng)建匿名類型:new { e.Name, color = e.Attribute("color") };

輸出結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-509715.html

{ Name = first, color = color="red" }
{ Name = second, color = color="red" }
{ Name = third, color = color="blue" }

first ,    color: red
second,    color: red
third ,    color: blue

到了這里,關(guān)于(十一)CSharp-LINQ-LINQToXML(4)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • (十一)地理數(shù)據(jù)庫創(chuàng)建——?jiǎng)?chuàng)建新的地理數(shù)據(jù)庫

    (十一)地理數(shù)據(jù)庫創(chuàng)建——?jiǎng)?chuàng)建新的地理數(shù)據(jù)庫

    地理數(shù)據(jù)庫(Geodatabase)是按照層次型的數(shù)據(jù)對象來組織地理數(shù)據(jù),這些數(shù)據(jù)對象包括對象類(Object Classes)、要素類(Feature Classes)和要素?cái)?shù)據(jù)集(Feature dataset)。 對象類是指存儲非空間數(shù)據(jù)的表格 (Table)。 要素類是具有相同幾何類型和屬性的要素的集合,即同類空間要素

    2024年02月05日
    瀏覽(14)
  • OpenCV:創(chuàng)建窗口,圖片顯示和保存

    OpenCV:創(chuàng)建窗口,圖片顯示和保存

    目錄 cv2.waitKey() 窗口創(chuàng)建:cv2.namedWindow() 窗口大小調(diào)整:cv2.resizeWindow() 窗口顯示:cv2.imshow() 關(guān)閉窗口:cv2.destroyAllWindows() 圖片讀?。篶v2.imread() 圖片保存:cv2.imwrite() 代碼示例 waitKey()的基本邏輯:他會在一定時(shí)間內(nèi)等待接收鍵盤的一個(gè)值; 返回值 為鍵盤按鍵的ASCII值; dela

    2024年01月19日
    瀏覽(20)
  • Java 中的線程是什么,如何創(chuàng)建和管理線程-上(十一)

    Java 中的線程是指程序中可以獨(dú)立執(zhí)行的最小單位。在 Java 中,創(chuàng)建線程通常有兩種方式:繼承 Thread 類和實(shí)現(xiàn) Runnable 接口。線程的管理包括控制線程狀態(tài)、線程優(yōu)先級、線程同步等。 一、Java 中的線程 線程是程序中能夠獨(dú)立執(zhí)行的最小單位,它具有自己的執(zhí)行路徑、調(diào)用棧

    2024年02月03日
    瀏覽(19)
  • Threejs進(jìn)階之十一:使用FontLoader和TextGeometry創(chuàng)建三維文字

    Threejs進(jìn)階之十一:使用FontLoader和TextGeometry創(chuàng)建三維文字

    在Threejs中我們可以通過FontLoader和TextGeometry結(jié)合使用來創(chuàng)建三維文字,F(xiàn)ontLoader用于加載JSON格式的字體,F(xiàn)ontLoader返回值是表示字體的Shape類型的數(shù)組;TextGeometry用于將文本生成為單一的幾何體。下面我們先來了解下這兩個(gè)類 用于加載JSON格式的字體的類。返回font, 返回值是表示

    2024年02月06日
    瀏覽(24)
  • solr快速上手:搭建solr集群并創(chuàng)建核心,設(shè)置數(shù)據(jù)同步(十一)

    solr快速上手:搭建solr集群并創(chuàng)建核心,設(shè)置數(shù)據(jù)同步(十一)

    前幾章我們已經(jīng)講解了solr單機(jī)版的基本使用,但實(shí)際生產(chǎn)中,為了保證高可用、高性能,我們一般會采用集群模式,所以接下來,我們繼續(xù)講解solr集群的搭建和基本操作 在講解solr集群模式前,我們要先了解“分片”的概念。 當(dāng)節(jié)點(diǎn)由一個(gè)拓展為多個(gè)時(shí),數(shù)據(jù)存儲和同步問

    2024年02月13日
    瀏覽(28)
  • 成本中心修改或者創(chuàng)建保存時(shí)增強(qiáng)的實(shí)現(xiàn)

    成本中心修改或者創(chuàng)建保存時(shí)增強(qiáng)的實(shí)現(xiàn)

    成本中心修改或者創(chuàng)建保存時(shí)增強(qiáng)?用戶出口程序?yàn)椋篍XIT_SAPLKMA1_003。 可以通過SMOD?或者?CMOD來添加增強(qiáng)代碼。 CMOD-COOMKS02 -EXIT_SAPLKMA1_003 按公司要求,寫了段代碼檢查 創(chuàng)建利成本中心時(shí),業(yè)務(wù)范圍要規(guī)范輸入。 ? ?效果如下: ?

    2024年02月15日
    瀏覽(13)
  • Outlook提示無法保存該附件。無法創(chuàng)建文件......

    Outlook提示無法保存該附件。無法創(chuàng)建文件........ 在Outlook中打開附件或者是下載附件的時(shí)候,彈出提示信息:無法保存該附件。無法創(chuàng)建文件:****。請右鍵單擊要在其中創(chuàng)建文件的文件夾,然后單擊快捷菜 單上的“屬性”,檢查對該文件夾的權(quán)限。 或者提示:Cannot create fi

    2024年02月04日
    瀏覽(21)
  • (七)CSharp-CSharp圖解教程版-事件

    (七)CSharp-CSharp圖解教程版-事件

    發(fā)布者/訂閱者模式(publish/subscriber pattern): 很多程序都有一個(gè)共同的需求,即當(dāng)一個(gè)特定的程序事件發(fā)生時(shí),程序的其他部分可以得到該事件已經(jīng)發(fā)生的通知。 發(fā)布者: 發(fā)布者類定義了一系列程序的其他部分可能感興趣的事件。 發(fā)布某個(gè)事件的類或結(jié)構(gòu),其他類可以在該

    2024年02月08日
    瀏覽(11)
  • opencv+圖像處理(GUI)1-0圖像:創(chuàng)建加載顯示保存關(guān)閉

    opencv+圖像處理(GUI)1-0圖像:創(chuàng)建加載顯示保存關(guān)閉

    本專欄代碼地址 https://github.com/xiawei20161308104/xv_opencv_tutorials 本節(jié)代碼路徑 xv_opencv_tutorials/ImageProcessinginOpenCV/load_img.py 創(chuàng)建窗口 namedWindow 從本地加載圖像 imread 在窗口中展示圖像 imshow 將圖像寫入文件 imwrite 關(guān)閉窗口 destroyWindow destroyAllWindows opencv提供 cv.namedWindow 函數(shù)實(shí)現(xiàn)創(chuàng)建一

    2023年04月21日
    瀏覽(24)
  • 創(chuàng)建圖書表book,保存圖書的信息,要求使用InnoDB引擎存儲

    創(chuàng)建圖書表book,保存圖書的信息,要求使用InnoDB引擎存儲

    我們基于圖書館管理數(shù)據(jù)庫dblibrary,現(xiàn)需要在該數(shù)據(jù)庫中完成創(chuàng)建和管理表的操作。 操作項(xiàng)目如下: 先用MySQL8.0環(huán)境 (1)創(chuàng)建圖書表book,保存圖書的信息。圖書表book結(jié)構(gòu)如下表所示。要求使用InnoDB引擎存儲。 圖書表book結(jié)構(gòu) 字段名稱 字段內(nèi)容 數(shù)據(jù)類型 長度 說明 Bookid 圖

    2024年02月08日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包