Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ==>

HTML
<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>



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
C#
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;
                }
Posted

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.
 
Share this answer
 
Comments
RaisKazi 3-Nov-11 9:34am    
My 5! Yes case-sensitive could be one of issue. I am probably feeling sleepy today. :)
Syed Salman Raza Zaidi 3-Nov-11 15:18pm    
I've tried by taking care of case sensitive but still having the same problem :(
Andrew Rissing 3-Nov-11 22:05pm    
You also need to handle the namespace issue.
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);


Returns your operationrequest node.
 
Share this answer
 
I am no expert in XML but a little bit of debugging showed me that ItemSearchResponse is the DocumentElement[^] property of your XML tree.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900