473,705 Members | 2,417 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 4978
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
5840
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
5804
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
1659
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
2403
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
2282
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
7734
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
4133
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
8767
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
8689
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9273
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
9030
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
8979
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
4439
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
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
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
2486
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.