Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

making XPATH with namespace in the root

Posted on 2007-11-29
8
Medium Priority
?
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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