473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:thing-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.Collections.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.XmlReader xmlReader = new
System.Xml.XmlTextReader(System.IO.File.OpenRead(s zFilename));

System.Xml.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace(String.Empty, "urn:thing-schema-v1");

System.Xml.XPath.XPathDocument xpath = new
System.Xml.XPath.XPathDocument(xmlReader);

System.Xml.XPath.XPathNavigator xpathNavi =
xpath.CreateNavigator();

System.Xml.XPath.XPathNodeIterator xpathQuery =
xpathNavi.Select(szXPath);
while(xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XPath.XPathExpression xpathExpr =
xpathNavi.Compile(szXPath);
xpathExpr.SetContext(xmlNsMgr);

xpathQuery = xpathNavi.Select(xpathExpr);
while (xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XmlDocument xmlDoc = new
System.Xml.XmlDocument();
xmlDoc.Load(szFilename);

System.Xml.XmlNamespaceManager xmlNsMgr2 = new
System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
xmlNsMgr2.AddNamespace(String.Empty,
"urn:thing-schema-v1");

System.Xml.XmlNodeList xmlNodes =
xmlDoc.SelectNodes(szXPath);

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

Dec 3 '06 #1
3 4945
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.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace("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:thing-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.Collections.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.XmlReader xmlReader = new
System.Xml.XmlTextReader(System.IO.File.OpenRead(s zFilename));

System.Xml.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace(String.Empty, "urn:thing-schema-v1");

System.Xml.XPath.XPathDocument xpath = new
System.Xml.XPath.XPathDocument(xmlReader);

System.Xml.XPath.XPathNavigator xpathNavi =
xpath.CreateNavigator();

System.Xml.XPath.XPathNodeIterator xpathQuery =
xpathNavi.Select(szXPath);
while(xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XPath.XPathExpression xpathExpr =
xpathNavi.Compile(szXPath);
xpathExpr.SetContext(xmlNsMgr);

xpathQuery = xpathNavi.Select(xpathExpr);
while (xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XmlDocument xmlDoc = new
System.Xml.XmlDocument();
xmlDoc.Load(szFilename);

System.Xml.XmlNamespaceManager xmlNsMgr2 = new
System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
xmlNsMgr2.AddNamespace(String.Empty,
"urn:thing-schema-v1");

System.Xml.XmlNodeList xmlNodes =
xmlDoc.SelectNodes(szXPath);

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

}
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"which 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.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace("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:thing-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.Collections.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.XmlReader xmlReader = new
System.Xml.XmlTextReader(System.IO.File.OpenRead(s zFilename));

System.Xml.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace(String.Empty, "urn:thing-schema-v1");

System.Xml.XPath.XPathDocument xpath = new
System.Xml.XPath.XPathDocument(xmlReader);

System.Xml.XPath.XPathNavigator xpathNavi =
xpath.CreateNavigator();

System.Xml.XPath.XPathNodeIterator xpathQuery =
xpathNavi.Select(szXPath);
while(xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XPath.XPathExpression xpathExpr =
xpathNavi.Compile(szXPath);
xpathExpr.SetContext(xmlNsMgr);

xpathQuery = xpathNavi.Select(xpathExpr);
while (xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}

System.Xml.XmlDocument xmlDoc = new
System.Xml.XmlDocument();
xmlDoc.Load(szFilename);

System.Xml.XmlNamespaceManager xmlNsMgr2 = new
System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
xmlNsMgr2.AddNamespace(String.Empty,
"urn:thing-schema-v1");

System.Xml.XmlNodeList xmlNodes =
xmlDoc.SelectNodes(szXPath);

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

}

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...@discussions.microsoft.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"which 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.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace("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:thing-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.Collections.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.XmlReader xmlReader = new
System.Xml.XmlTextReader(System.IO.File.OpenRead(s zFilename));
System.Xml.XmlNamespaceManager xmlNsMgr = new
System.Xml.XmlNamespaceManager(xmlReader.NameTable );
xmlNsMgr.AddNamespace(String.Empty, "urn:thing-schema-v1");
System.Xml.XPath.XPathDocument xpath = new
System.Xml.XPath.XPathDocument(xmlReader);
System.Xml.XPath.XPathNavigator xpathNavi =
xpath.CreateNavigator();
System.Xml.XPath.XPathNodeIterator xpathQuery =
xpathNavi.Select(szXPath);
while(xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}
System.Xml.XPath.XPathExpression xpathExpr =
xpathNavi.Compile(szXPath);
xpathExpr.SetContext(xmlNsMgr);
xpathQuery = xpathNavi.Select(xpathExpr);
while (xpathQuery.MoveNext())
{
string szValue = xpathQuery.Current.Value;
System.Console.WriteLine(szValue);
}
System.Xml.XmlDocument xmlDoc = new
System.Xml.XmlDocument();
xmlDoc.Load(szFilename);
System.Xml.XmlNamespaceManager xmlNsMgr2 = new
System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
xmlNsMgr2.AddNamespace(String.Empty,
"urn:thing-schema-v1");
System.Xml.XmlNodeList xmlNodes =
xmlDoc.SelectNodes(szXPath);
foreach(System.Xml.XmlNode xmlNode in xmlNodes)
{
string szValue = xmlNode.Value;
System.Console.WriteLine(szValue);
}
}
}
}
Dec 3 '06 #4

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

Similar topics

4
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
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 ...
2
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 -...
2
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...
5
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...
10
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...
18
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);...
3
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 =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.