C#中通過xpath查找xml的指定元素

b5cw 9年前發布 | 820 次閱讀 C#

orders.xml文檔內容如下

<?xml version="1.0"?>
<Order id="2004-01-30.195496">
  <Client id="ROS-930252034">
    <Name>Remarkable Office Supplies</Name>
  </Client>

  <Items>
    <Item id="1001">
      <Name>Electronic Protractor</Name>
      <Price>42.99</Price>
    </Item>
    <Item id="1002">
      <Name>Invisible Ink</Name>
      <Price>200.25</Price>
    </Item>
  </Items>
</Order>



//該代碼片段來自于: http://www.sharejs.com/codes/csharp/7775
C#代碼
using System;
using System.Xml;

public class XPathSelectNodes {
    private static void Main() {

        // Load the document.
        XmlDocument doc = new XmlDocument();
        doc.Load("orders.xml");
        XmlNodeList nodes = doc.SelectNodes("/Order/Items/Item/Name");

        foreach (XmlNode node in nodes) {
            Console.WriteLine(node.InnerText);
        }

        Console.ReadLine();
    }
}


//該代碼片段來自于: http://www.sharejs.com/codes/csharp/7775
輸出結果
Electronic Protractor
Invisible Ink
 本文由用戶 b5cw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!