Link to home
Start Free TrialLog in
Avatar of eladr
eladr

asked on

making XPATH with namespace in the root

Hi...
im getting NULL in the value of the xmlnode.
when i delete the namespace is working.
how can i get the value? should i use namespace manager?
i prefer that xpath wont contain the namespaces.
i need to know if there is at least one "status" element with "false" value.
10x

XmlDocument xmldoc = new XmlDocument();
            string xml = "<UpdateXIResponse xmlns='http://Global.Schemas.ScmUpdateXIResponse'><interface_seq>interface_seq_0</interface_seq><interface_name>interface_name_0</interface_name><interface_datetime>interface_datetime_0</interface_datetime><Data><Seq_num>Seq_num_0</Seq_num><Ifc_Date>Ifc_Date_0</Ifc_Date><Status>False</Status><Failure_code>Failure_code_0</Failure_code><Failure_Reason>Failure_Reason_0</Failure_Reason>";
            xml += "</Data><Data><Seq_num>Seq_num_0</Seq_num><Ifc_Date>Ifc_Date_0</Ifc_Date><Status>True</Status><Failure_code>Failure_code_0</Failure_code><Failure_Reason>Failure_Reason_0</Failure_Reason></Data></UpdateXIResponse>";
            xmldoc.LoadXml(xml);

       
            XmlNode xNode = xmldoc.SelectSingleNode("//UpdateXIResponse/Data[Status='False']");
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

yes,

you have three optons
- change the source XML and remove the namespace
- use a namespace manager
- make your XPath namespace independent:
              //*[local-name() = 'UpdateXIResponse']/*[local-name() = 'Data'][*[local-name() = 'Status']/text()='False']

+ the namespace independent XPath is slower
+ I don't like changing a source XML
+ namespace manager is the cleanest solution

cheers

Geert
Avatar of eladr
eladr

ASKER

how can i use the namespace manager?...
can u send me some simple example?
Avatar of eladr

ASKER

in the addnamespace method i need 2 supply prefix which i dont have in the document...
the namespace is global.
if there isn't a prefix, the prefix is the empty string
Avatar of eladr

ASKER

Not working...

here the code:

XmlDocument xmldoc = new XmlDocument();
            string xml = "<UpdateXIResponse xmlns='http://CoIns.Global.Schemas.ScmUpdateXIResponse'><interface_seq>interface_seq_0</interface_seq><interface_name>interface_name_0</interface_name><interface_datetime>interface_datetime_0</interface_datetime><Data><Seq_num>Seq_num_0</Seq_num><Ifc_Date>Ifc_Date_0</Ifc_Date><Status>False</Status><Failure_code>Failure_code_0</Failure_code><Failure_Reason>Failure_Reason_0</Failure_Reason>";
            xml += "</Data><Data><Seq_num>Seq_num_0</Seq_num><Ifc_Date>Ifc_Date_0</Ifc_Date><Status>True</Status><Failure_code>Failure_code_0</Failure_code><Failure_Reason>Failure_Reason_0</Failure_Reason></Data></UpdateXIResponse>";
            xmldoc.LoadXml(xml);
 
            XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xmldoc.NameTable);
            xmlnsm.AddNamespace(string.Empty,"http://CoIns.Global.Schemas.ScmUpdateXIResponse");
            int linesCount = xmldoc.SelectNodes("//UpdateXIResponse/Data", xmlnsm).Count;
            XmlNode xNode = xmldoc.SelectSingleNode("//UpdateXIResponse/Data[Status='False']",xmlnsm);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eladr

ASKER

thanks