Solved

making XPATH with namespace in the root

Posted on 2007-11-29
8
566 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20373158
if there isn't a prefix, the prefix is the empty string
0
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

815 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

8 Experts available now in Live!

Get 1:1 Help Now