LINQ to XML 進階篇(二)

來源:互聯網
上載者:User
/**/////檢索元素的值
XElement e = new XElement("StringElement", "abcd");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + (string)e);
Console.WriteLine("Value of e by Value:"+e.Value);

Code
//檢索元素集合
            XElement po = XElement.Load("PurchaseOrder.xml");
            IEnumerable<XElement> childElements =
                from el in po.Elements()
                select el;
            foreach (XElement el in childElements)
                Console.WriteLine("Name: " + el.Name);

Code
////根據元素的名稱進行篩選
            XElement po =XElement.Load("PurchaseOrder.xml");
            IEnumerable<XElement> items =
                from el in po.Descendants("ProductName")
                select el;
            foreach (XElement proName in items)
                Console.Write("PrdName" + ":" + (string)proName);

Code
////根據元素的名稱進行篩選(有命名空間)
            XNamespace aw = "http://www.adventure-works.com";
            XElement po = XElement.Load("PurchaseOrderInNamespace.xml");
            IEnumerable<XElement> items =
                from el in po.Descendants(aw + "ProductName")
                select el;
            foreach (XElement prdName in items)
                Console.WriteLine(prdName.Name + ":" + (string)prdName);

Code
 ////連結軸方法,有時,當可能存在或不存在間隔上級時,您希望在特定的元素深度,檢索所有的元素
            XElement root = XElement.Load("Irregular.xml");
            IEnumerable<XElement> configParameters =
                root.Elements("Customer").Elements("Config").
                Elements("ConfigParameter");
            foreach (XElement cp in configParameters)
                Console.WriteLine(cp.Value);

////檢索單個子項目
            XElement po = XElement.Load("PurchaseOrder.xml");
            XElement e = po.Element("DeliveryNotes");
            Console.WriteLine(e);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.