473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple xpath query with default namespace

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:thin g-schema-v1">
<foo>foo thing</foo>
<bar>bar thing</bar>
<baz>baz thing</baz>
</thing>

....everything in the code below fails (as in no nodes are located, when
3 should be). However, if I remove the xmlns declaration from the
<thingroot element everything works fine. What am I doing wrong?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
const string szXPath = "/thing/*";
const string szFilename = @"..\..\test-wo-xmlns.xml";

System.Xml.XmlR eader xmlReader = new
System.Xml.XmlT extReader(Syste m.IO.File.OpenR ead(szFilename) );

System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace(String.E mpty, "urn:thing-schema-v1");

System.Xml.XPat h.XPathDocument xpath = new
System.Xml.XPat h.XPathDocument (xmlReader);

System.Xml.XPat h.XPathNavigato r xpathNavi =
xpath.CreateNav igator();

System.Xml.XPat h.XPathNodeIter ator xpathQuery =
xpathNavi.Selec t(szXPath);
while(xpathQuer y.MoveNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XPat h.XPathExpressi on xpathExpr =
xpathNavi.Compi le(szXPath);
xpathExpr.SetCo ntext(xmlNsMgr) ;

xpathQuery = xpathNavi.Selec t(xpathExpr);
while (xpathQuery.Mov eNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XmlD ocument xmlDoc = new
System.Xml.XmlD ocument();
xmlDoc.Load(szF ilename);

System.Xml.XmlN amespaceManager xmlNsMgr2 = new
System.Xml.XmlN amespaceManager (xmlDoc.NameTab le);
xmlNsMgr2.AddNa mespace(String. Empty,
"urn:thing-schema-v1");

System.Xml.XmlN odeList xmlNodes =
xmlDoc.SelectNo des(szXPath);

foreach(System. Xml.XmlNode xmlNode in xmlNodes)
{
string szValue = xmlNode.Value;
System.Console. WriteLine(szVal ue);
}
}
}
}

Dec 3 '06 #1
3 4979
I don't quite understand why this works... but as many others have
probably discovered if you create a namespace manager like so:

System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace("q", "urn:thing-schema-v1");

....then use a query such as "/q:thing/*" then everything in the code
below works. Why is this?

On Dec 2, 11:17 pm, "Jason Mobarak" <jason.moba...@ gmail.comwrote:
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:thin g-schema-v1">
<foo>foo thing</foo>
<bar>bar thing</bar>
<baz>baz thing</baz>
</thing>

...everything in the code below fails (as in no nodes are located, when
3 should be). However, if I remove the xmlns declaration from the
<thingroot element everything works fine. What am I doing wrong?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
const string szXPath = "/thing/*";
const string szFilename = @"..\..\test-wo-xmlns.xml";

System.Xml.XmlR eader xmlReader = new
System.Xml.XmlT extReader(Syste m.IO.File.OpenR ead(szFilename) );

System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace(String.E mpty, "urn:thing-schema-v1");

System.Xml.XPat h.XPathDocument xpath = new
System.Xml.XPat h.XPathDocument (xmlReader);

System.Xml.XPat h.XPathNavigato r xpathNavi =
xpath.CreateNav igator();

System.Xml.XPat h.XPathNodeIter ator xpathQuery =
xpathNavi.Selec t(szXPath);
while(xpathQuer y.MoveNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XPat h.XPathExpressi on xpathExpr =
xpathNavi.Compi le(szXPath);
xpathExpr.SetCo ntext(xmlNsMgr) ;

xpathQuery = xpathNavi.Selec t(xpathExpr);
while (xpathQuery.Mov eNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XmlD ocument xmlDoc = new
System.Xml.XmlD ocument();
xmlDoc.Load(szF ilename);

System.Xml.XmlN amespaceManager xmlNsMgr2 = new
System.Xml.XmlN amespaceManager (xmlDoc.NameTab le);
xmlNsMgr2.AddNa mespace(String. Empty,
"urn:thing-schema-v1");

System.Xml.XmlN odeList xmlNodes =
xmlDoc.SelectNo des(szXPath);

foreach(System. Xml.XmlNode xmlNode in xmlNodes)
{
string szValue = xmlNode.Value;
System.Console. WriteLine(szVal ue);
}
}
}

}
Dec 3 '06 #2
Hi Jason,
namespaces in XML serve the same purpose as namespace in C#, they are a
way to distinguish between two items that happen to have the same identifier.
In XML you may have one XML definition that has a node called <Account
BankNo="123"whi ch refers to a Bank Account and another node that is also
called <Account Name="bob"that refers to a Video Rental account, so if both
nodes are part of the same XML document how could you distinguish between
them? The namespace allows that since one could be part of the namespace
http://www.example.org/banking and another http://www.example.org/videorental
then when you have your XPath query you will need to tell XPath which Account
element you mean by prefixing the name with the namespace in your query.
Hence in your example because the node is part of a namespace you need to use
that information in your XPath query.
Mark.
--
http://www.markdawson.org
"Jason Mobarak" wrote:
I don't quite understand why this works... but as many others have
probably discovered if you create a namespace manager like so:

System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace("q", "urn:thing-schema-v1");

....then use a query such as "/q:thing/*" then everything in the code
below works. Why is this?

On Dec 2, 11:17 pm, "Jason Mobarak" <jason.moba...@ gmail.comwrote:
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:thin g-schema-v1">
<foo>foo thing</foo>
<bar>bar thing</bar>
<baz>baz thing</baz>
</thing>

...everything in the code below fails (as in no nodes are located, when
3 should be). However, if I remove the xmlns declaration from the
<thingroot element everything works fine. What am I doing wrong?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
const string szXPath = "/thing/*";
const string szFilename = @"..\..\test-wo-xmlns.xml";

System.Xml.XmlR eader xmlReader = new
System.Xml.XmlT extReader(Syste m.IO.File.OpenR ead(szFilename) );

System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace(String.E mpty, "urn:thing-schema-v1");

System.Xml.XPat h.XPathDocument xpath = new
System.Xml.XPat h.XPathDocument (xmlReader);

System.Xml.XPat h.XPathNavigato r xpathNavi =
xpath.CreateNav igator();

System.Xml.XPat h.XPathNodeIter ator xpathQuery =
xpathNavi.Selec t(szXPath);
while(xpathQuer y.MoveNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XPat h.XPathExpressi on xpathExpr =
xpathNavi.Compi le(szXPath);
xpathExpr.SetCo ntext(xmlNsMgr) ;

xpathQuery = xpathNavi.Selec t(xpathExpr);
while (xpathQuery.Mov eNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}

System.Xml.XmlD ocument xmlDoc = new
System.Xml.XmlD ocument();
xmlDoc.Load(szF ilename);

System.Xml.XmlN amespaceManager xmlNsMgr2 = new
System.Xml.XmlN amespaceManager (xmlDoc.NameTab le);
xmlNsMgr2.AddNa mespace(String. Empty,
"urn:thing-schema-v1");

System.Xml.XmlN odeList xmlNodes =
xmlDoc.SelectNo des(szXPath);

foreach(System. Xml.XmlNode xmlNode in xmlNodes)
{
string szValue = xmlNode.Value;
System.Console. WriteLine(szVal ue);
}
}
}

}

Dec 3 '06 #3
Thanks Mark. You're explanation makes sense. I guess I was confused
by the fact that I needed to introduce a prefix in to the my xpath
query (a prefix that's no actually in the xml) in order for the library
to allow me to query for something in a namespace, regardless of
whether it's the default namespace.

On Dec 3, 12:45 am, Mark R. Dawson
<MarkRDaw...@di scussions.micro soft.comwrote:
Hi Jason,
namespaces in XML serve the same purpose as namespace in C#, they are a
way to distinguish between two items that happen to have the same identifier.
In XML you may have one XML definition that has a node called <Account
BankNo="123"whi ch refers to a Bank Account and another node that is also
called <Account Name="bob"that refers to a Video Rental account, so if both
nodes are part of the same XML document how could you distinguish between
them? The namespace allows that since one could be part of the namespacehttp://www.example.org/bankingand anotherhttp://www.example.org/videorental
then when you have your XPath query you will need to tell XPath which Account
element you mean by prefixing the name with the namespace in your query.
Hence in your example because the node is part of a namespace you need to use
that information in your XPath query.

Mark.
--http://www.markdawson. org

"Jason Mobarak" wrote:
I don't quite understand why this works... but as many others have
probably discovered if you create a namespace manager like so:
System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace("q", "urn:thing-schema-v1");
....then use a query such as "/q:thing/*" then everything in the code
below works. Why is this?
On Dec 2, 11:17 pm, "Jason Mobarak" <jason.moba...@ gmail.comwrote:
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:thin g-schema-v1">
<foo>foo thing</foo>
<bar>bar thing</bar>
<baz>baz thing</baz>
</thing>
...everything in the code below fails (as in no nodes are located, when
3 should be). However, if I remove the xmlns declaration from the
<thingroot element everything works fine. What am I doing wrong?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
const string szXPath = "/thing/*";
const string szFilename = @"..\..\test-wo-xmlns.xml";
System.Xml.XmlR eader xmlReader = new
System.Xml.XmlT extReader(Syste m.IO.File.OpenR ead(szFilename) );
System.Xml.XmlN amespaceManager xmlNsMgr = new
System.Xml.XmlN amespaceManager (xmlReader.Name Table);
xmlNsMgr.AddNam espace(String.E mpty, "urn:thing-schema-v1");
System.Xml.XPat h.XPathDocument xpath = new
System.Xml.XPat h.XPathDocument (xmlReader);
System.Xml.XPat h.XPathNavigato r xpathNavi =
xpath.CreateNav igator();
System.Xml.XPat h.XPathNodeIter ator xpathQuery =
xpathNavi.Selec t(szXPath);
while(xpathQuer y.MoveNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}
System.Xml.XPat h.XPathExpressi on xpathExpr =
xpathNavi.Compi le(szXPath);
xpathExpr.SetCo ntext(xmlNsMgr) ;
xpathQuery = xpathNavi.Selec t(xpathExpr);
while (xpathQuery.Mov eNext())
{
string szValue = xpathQuery.Curr ent.Value;
System.Console. WriteLine(szVal ue);
}
System.Xml.XmlD ocument xmlDoc = new
System.Xml.XmlD ocument();
xmlDoc.Load(szF ilename);
System.Xml.XmlN amespaceManager xmlNsMgr2 = new
System.Xml.XmlN amespaceManager (xmlDoc.NameTab le);
xmlNsMgr2.AddNa mespace(String. Empty,
"urn:thing-schema-v1");
System.Xml.XmlN odeList xmlNodes =
xmlDoc.SelectNo des(szXPath);
foreach(System. Xml.XmlNode xmlNode in xmlNodes)
{
string szValue = xmlNode.Value;
System.Console. WriteLine(szVal ue);
}
}
}
}
Dec 3 '06 #4

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

Similar topics

4
5842
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
1
5808
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
2881
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
1664
by: MAF | last post by:
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.XmlDocument(); if (this.m_FileLocation != null && this.m_FileLocation.Length > 0) XMLDoc.Load(this.m_FileLocation);
5
2406
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new XmlNamespaceManager(xml.NameTable); context.AddNamespace("", "http://www.test.org"); context.AddNamespace("sns", "http://www.test.org/sub"); XmlNode node = xml.SelectSingleNode("/root", context);
10
2285
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>
18
7737
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
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
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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
8239
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
6073
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
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.