473,763 Members | 5,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPath Query problem with Namespace

MAF
I am triing to query the following xml document. I am loading the xml into
a XML Document and then using XPath to get the EntityDataSet Node

Here is my code

XMLDoc = new System.Xml.XmlD ocument();

if (this.m_FileLoc ation != null && this.m_FileLoca tion.Length > 0)

XMLDoc.Load(thi s.m_FileLocatio n);

else

{

throw new System.IO.FileN otFoundExceptio n("File can not be found",
this.m_FileLoca tion);

}

//Locate Root

System.Xml.XmlN odeList rootList = XMLDoc.SelectNo des("//EntityDataSet") ;
//This returns nothing

If I remvoe the xmlns attribute of EntityTree I do get the desired note.
What do I need to do to my XPath to make this work properly?
<?xml version="1.0"?>

<EntityTreeColl ection ExportDateTime= "2/14/2005 4:57:31 PM"
SystemName="tes tXP" UserName="test" UserID="45">

<EntityTree xmlns="http://acme.com/EntityTree.xsd"
ExportedFromDat aModelVersion=" 4.9.0" EntityCount="7" >

<EntityTreeNo de Selected="true" >

<EntityDataSe t>

<Entity EntityID="10128 " EntityName="INT LOAD"/>

</EntityDataSet>

</EntityTreeNode>

</EntityTree>

</EntityTreeColle ction>
Nov 12 '05 #1
2 1664
Your XPath is looking for a node called EntityDataSet whose namespace uri is
empty, while the node you're searching from is qualified by the namespace
"http://acme.com/EntityTree.xsd" .

The easy solution is to select the node by local name:
http://msdn.microsoft.com/library/de...mefunction.asp
http://www.w3.org/TR/xpath#function-local-name

System.Xml.XmlN odeList rootList = XMLDoc.SelectNo des("//*[local-name() =
'EntityDataSet']");

The serious solution is to use namespace manager:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...odesTopic2.asp

System.Xml.XmlN amespaceManager Manager = new
System.Xml.XmlN amespaceManager (XMLDoc.NameTab le);
Manager.AddName space("x", "http://acme.com/EntityTree.xsd" );

System.Xml.XmlN odeList rootList = XMLDoc.SelectNo des("//x:EntityDataSet ",
Manager);
Nov 12 '05 #2


MAF wrote:
I am triing to query the following xml document. I am loading the xml into
a XML Document and then using XPath to get the EntityDataSet Node If I remvoe the xmlns attribute of EntityTree I do get the desired note.
What do I need to do to my XPath to make this work properly?
<?xml version="1.0"?>

<EntityTreeColl ection ExportDateTime= "2/14/2005 4:57:31 PM"
SystemName="tes tXP" UserName="test" UserID="45">

<EntityTree xmlns="http://acme.com/EntityTree.xsd"
ExportedFromDat aModelVersion=" 4.9.0" EntityCount="7" >


You need to bind a prefix to the default namespace and use the prefix in
the XPath expression, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2375
by: Chris | last post by:
Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in specific elements within my XML file. The problem I am getting is that one of the XML elements contains namespace attributes and this seems to prevent the Xpath query from working (ie, if I manually strip the namespace attributes then the query works...
6
5984
by: 0wl | last post by:
Hi, I am trying to get the value of child from xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child DataType="String">Hellpppp</p:child></p:root>""" using doc=parseString(xmlstr) nodeList = xml.xpath.Evaluate("/p:root/p:child/text()", doc)
4
3045
by: MarcoT77 | last post by:
Hi Teacher. I'm trying to get with Xpath the Product nodes in the following XML: <?xml version="1.0" encoding="utf-8"?> <PLMXML xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema" schemaVersion="5.1" date="2005-05-03" time="16:41:33" author="TcEng - infodba@bridge(389284449)"> <Header id="id1" traverseRootRefs="#id2 #id11" transferContext="exp_folder"></Header> <Product id="id10" name="Ace" accessRefs="#id3" subType="Drawing"
1
5809
by: Robert | last post by:
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code Dim nodess As XmlNode nodess = myNode.SelectSingleNode("//Web") If nodess Is Nothing Then SQLrw.Delete()
2
2882
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file - "xmlns="http://tempuri.org/nameOfRoot.xsd" I have no idea what its pointing to & what is tempuri.org? So when this tag is in my xml tag my xpath query never works. But when I delete it work fine.
2
10050
by: dc | last post by:
i have a xml file like this: <?xml version="1.0" encoding="utf-8"?> <validate xmlns="http://tempuri.org/fieldValidate.xsd"> <field name="Short Name" type="SN" length="10"> <requiredChar value="Y" errMsg="required Y" /> <requiredChar value="X" errMsg="required X" /> <bannedChar value="Z" errMsg="banned Z" /> <bannedChar value="A" errMsg="banned A" /> </field>
10
2288
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the field names. Everything there works fine. Here's my XML Document: <?xml version="1.0" standalone="yes" ?> <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> <tblItem>
3
4980
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml version="1.0" encoding="utf-8" ?> <thing xmlns="urn:thing-schema-v1"> <foo>foo thing</foo> <bar>bar thing</bar>
3
4136
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(str); request.Credentials = System.Net.CredentialCache.DefaultCredentials;
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.