Solved

making XPATH with namespace in the root

Posted on 2007-11-29
8
564 Views
Last Modified: 2010-04-21
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']");
0
Comment
Question by:eladr
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20372887
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
0
 
LVL 1

Author Comment

by:eladr
ID: 20373012
how can i use the namespace manager?...
can u send me some simple example?
0
 
LVL 1

Author Comment

by:eladr
ID: 20373145
in the addnamespace method i need 2 supply prefix which i dont have in the document...
the namespace is global.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20373158
if there isn't a prefix, the prefix is the empty string
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:eladr
ID: 20373174
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

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 100 total points
ID: 20373308
you could add a random prefix
(that is what usually happens)

xmlnsm.AddNamespace("resp","http://CoIns.Global.Schemas.ScmUpdateXIResponse");

XmlNode xNode = xmldoc.SelectSingleNode("//resp:UpdateXIResponse/resp:Data[resp:Status='False']",xmlnsm);
0
 
LVL 1

Author Closing Comment

by:eladr
ID: 31411637
thanks
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20373813
welcome
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now