简体   繁体   English

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

[英]How to read XML in C# using Xpath

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?. 如何使用XPath读取<SKUDetails>节点? What will be XNamespace for above XML? 以上XML的XNamespace是什么?

Manipulate XML data with XPath and XmlDocument (C#) 使用XPath和XmlDocument(C#)处理XML数据

or 要么

its better to use LINQ to XML as 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);
}

as @Kirill Polishchuk answered - 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 XmlDocument like this: 你可以像这样使用alsow 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);

SKUsDetails is defined in http://tempuri.org/ namespace. SKUsDetailshttp://tempuri.org/名称空间中定义。 You can use this code to select SKUsDetails using XPath: 您可以使用此代码使用XPath选择SKUsDetails

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 SKUDetails use: //a:SKUsDetails/a:SKUDetails 要选择SKUDetails使用: //a:SKUsDetails/a:SKUDetails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM