Solved

making XPATH with namespace in the root

Posted on 2007-11-29
8
565 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sum Column in GridView 3 44
Name space syntax error 12 44
Host asp.net pages 5 26
C#  Radio button search for Date not DateTime 4 35
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

18 Experts available now in Live!

Get 1:1 Help Now