如何使用 Xpath 在 C# 中读取 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9511608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:42:36  来源:igfitidea点击:

How to read XML in C# using Xpath

c#xml.net-4.0

提问by SOF User

I have this XML

我有这个 XML

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetSKUsPriceAndStockResponse xmlns="http://tempuri.org/">
      <GetSKUsPriceAndStockResult>
        <RequestStatus>
          <DateTime>2/28/2012 5:28:05 PM</DateTime>
          <Message>S200</Message>
        </RequestStatus>
        <SKUsDetails>
          <SKUDetails>
            <SKU>N82E16834230265</SKU>
            <Model>X54C-NS92</Model>
            <Stock>true</Stock>
            <Domain>newegg.com</Domain>
            <SalePrice>439.99</SalePrice>
            <ShippingCharge>0.00</ShippingCharge>
            <Currency>USD</Currency>
          </SKUDetails>
        </SKUsDetails>
      </GetSKUsPriceAndStockResult>
    </GetSKUsPriceAndStockResponse>
  </soap:Body>
</soap:Envelope>

How can I read <SKUDetails>Node using XPath?. What will be XNamespace for above XML?

如何<SKUDetails>使用 XPath读取节点?上面 XML 的 XNamespace 是什么?

回答by Pranay Rana

Manipulate XML data with XPath and XmlDocument (C#)

使用 XPath 和 XmlDocument (C#) 操作 XML 数据

or

或者

its better to useLINQ to XMLas your are using .net 4.0 and there is no need to learn XPath to traverse the xml tree.

最好使用LINQ to XML,因为您使用的是 .net 4.0,并且无需学习 XPath 来遍历 xml 树。

Not sure about the xpath expression but you can code like this

不确定 xpath 表达式,但您可以这样编码

string fileName = "data.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr; 
expr = nav.Compile("/GetSKUsPriceAndStockResponse/GetSKUsPriceAndStockResult/SKUsDetails/SKUDetails");
XPathNodeIterator iterator = nav.Select(expr);
try
{
  while (iterator.MoveNext())
  {

  }
}
catch(Exception ex) 
{
   Console.WriteLine(ex.Message);
}

回答by Kirill Polishchuk

SKUsDetailsis defined in http://tempuri.org/namespace. You can use this code to select SKUsDetailsusing XPath:

SKUsDetailshttp://tempuri.org/命名空间中定义。您可以使用此代码选择SKUsDetails使用 XPath:

var doc = XDocument.Load("1.xml");

var mgr = new XmlNamespaceManager(doc.CreateReader().NameTable);
mgr.AddNamespace("a", "http://tempuri.org/");

var node = doc.XPathSelectElement("//a:SKUsDetails", mgr);

To select SKUDetailsuse: //a:SKUsDetails/a:SKUDetails

选择SKUDetails使用://a:SKUsDetails/a:SKUDetails

回答by shmoltz

as @Kirill Polishchukanswered - SKUDetails is defined in http://tempuri.org/

正如@Kirill Polishchuk回答的那样-SKUDetails is defined in http://tempuri.org/

he shows you how to get using XDocument

他向您展示了如何使用 XDocument

you can use alsow XmlDocumentlike this:

你也可以这样使用XmlDocument

var dom = new XmlDocument();
dom.Load("data.xml");
var mgr = new XmlNamespaceManager(dom.NameTable);
mgr.AddNamespace("a", "http://tempuri.org/");
var res = dom.SelectNodes("//a:SKUDetails", mgr);