如何从Web服务中获取XML值的项目值
问题描述:
大家好,
我有一个返回此XML的网络服务
hello all,
I have a webservice that returns this XML
<UpdateSalesHeaderResponse xmlns="http://tempuri.org/">
<UpdateSalesHeaderResult>true</UpdateSalesHeaderResult>
</UpdateSalesHeaderResponse>
我如何获得价值UpdateSalesHeaderResult标签?
我目前这样做:
How can i get the value of UpdateSalesHeaderResult tag?
I currently do this:
XmlDocument doc = new XmlDocument();
// Load data
doc.Load("http://localhost/xxxxx/Service1.svc/xxxxxx");
//
if (doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText == "true")
{
//do smth
}
else
{
//do smth
}
错误说:对象引用未设置为对象的实例。此时doc.SelectSingleNode(// UpdateSalesHeaderResult)。InnerText
the error says :"Object reference not set to an instance of an object." at this point doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText
答
我认为是导致问题的命名空间(注意我还没有完全得到)他们可以随意改进这个答案)
I think it is the namespace causing an issue (note I don''t fully get them so feel free to improve this answer)
// Load data
doc.Load("http://localhost/xxxxx/Service1.svc/xxxxxx");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("tu", @"http://tempuri.org/");
if (doc.SelectSingleNode("//tu:UpdateSalesHeaderResult", nsmgr).InnerText == "true")
{
//do smth
}
else
{
//do smth
}
我最终使用了XMLReader:
I ended up using XMLReader:
XmlTextReader reader = new XmlTextReader("http://localhost/xxxxxxxx/Service1.svc/xxxxx<pre lang="cs">");
// Skip non-significant whitespace
reader.WhitespaceHandling = WhitespaceHandling.Significant;
// Read nodes one at a time
while (reader.Read())
{
if(reader.Value == "true")
{
//do smth
}
if(reader.Value == "false")
{
//do smth
}
}</pre>