XML文档问题 [英] XML Document Problem

查看:74
本文介绍了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;
                }

推荐答案

您在这里遇到了一些问题.使用SelectSingleNode时,必须意识到它区分大小写.其次,如果您在XML中定义了名称空间,则必须使用名称空间管理器才能正确访问元素.

看看 [
You have a few problems going on here. When working with SelectSingleNode, you have to realize it is case-sensitive. Secondly, if you have a namespace defined within the XML, you must use a namespace manager to be able to properly access the elements.

Take a look at this[^] for how to use the the namespace manager.


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方面的专家,但是一点点调试告诉我ItemSearchResponse
I am no expert in XML but a little bit of debugging showed me that ItemSearchResponse is the DocumentElement[^] property of your XML tree.


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

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