本文介绍了XML文档问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML文档有问题
当我选择节点时,它将引发异常Expression必须计算为节点集.

我的XML是==>

I''m having a problem in my XML document
When i''m selecting the nodes it is throwing an exception Expression must evaluate to a node-set.

My XML is ==>

<itemsearchresponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31">
    <operationrequest>
        <requestid>d5220d4f-5798-4d05-82b0-6af6f97f5f5f</requestid>
        <arguments>
            <argument name="Condition" value="All"></argument>
            <argument name="Operation" value="ItemSearch"></argument>
        </arguments>
        <requestprocessingtime>0.2157430000000000</requestprocessingtime>
    </operationrequest>
    <items>
</items></itemsearchresponse>




我正在尝试访问ItemSearchResponse节点
通过使用此代码

doc.SelectSingleNode("/ItemSearchResponse/");

我也尝试过

doc.SelectSingleNode("//ItemSearchResponse/");

但是发生了同样的事情

通过我的调查,我已经知道这是由于xmlns属性引起的,在删除该属性但没有任何效果后,我也尝试过使用它

完整的代码是




i''m trying to access ItemSearchResponse node
by using this code

doc.SelectSingleNode("/ItemSearchResponse/");

I also have tried

doc.SelectSingleNode("//ItemSearchResponse/");

but same thing happening

Through my investigation i''ve reached the reason that this is because of xmlns attribute i also tried it after removing this attribute but no working

Complete code is

XmlDocument doc = new XmlDocument();
                doc.Load(//my XML here);

                var node = doc.GetElementsByTagName("ItemSearchResponse")[0];
                var attrib = node.Attributes["xmlns"].Value;
                if(attrib == "http://webservices.amazon.com/AWSECommerceService/2009-03-31")
                {
                    node.Attributes["xmlns"].Value = "";
                    doc.InnerXml = node.OuterXml;
                }

推荐答案


System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(//your XML here);

System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlNamespaceManager nsmgr = new
System.Xml.XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", root.NamespaceURI); // x is our temp alias
System.Xml.XmlNode wptNode = root.SelectSingleNode("x:operationrequest", nsmgr);



返回您的operationrequest节点.



Returns your operationrequest node.



这篇关于XML文档问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:53