如何从Web服务中获取XML值的项目值 [英] How to get an item value from XMLreturned from webservice

查看:55
本文介绍了如何从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">&quot;);

              // Skip non-significant whitespace
              reader.WhitespaceHandling = WhitespaceHandling.Significant;

              // Read nodes one at a time
              while (reader.Read())
              {
                 if(reader.Value == &quot;true&quot;)
                 {
                     //do smth
                 }
              if(reader.Value == &quot;false&quot;)
                 {
                     //do smth
                 }

              }</pre>


这篇关于如何从Web服务中获取XML值的项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆