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?
如何使用XPath读取
3 个解决方案
#1
2
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);
}
#2
1
SKUsDetails
is defined in http://tempuri.org/
namespace. You can use this code to select SKUsDetails
using XPath:
SKUsDetails在http://tempuri.org/名称空间中定义。您可以使用此代码使用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
#3
1
as @Kirill Polishchuk answered - SKUDetails is defined in http://tempuri.org/
正如@Kirill Polishchuk所回答 - SKUDetails在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);
#1
2
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);
}
#2
1
SKUsDetails
is defined in http://tempuri.org/
namespace. You can use this code to select SKUsDetails
using XPath:
SKUsDetails在http://tempuri.org/名称空间中定义。您可以使用此代码使用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
#3
1
as @Kirill Polishchuk answered - SKUDetails is defined in http://tempuri.org/
正如@Kirill Polishchuk所回答 - SKUDetails在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);