473,378 Members | 1,388 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,378 software developers and data experts.

XMLNamespaceManager problem

I'm trying to query a XML file that was created via ADO.NET. My query
wasn't returning anything, and I tracked the problem to an issue with the
namespace that ADO.NET specified. When I remove the namespace from the XML
data, the query worked.

Okay, so I added code to create a XMLNamespaceManager and populate it with
the namespace. Now, I'm getting this exception:

Prefixes beginning with "xml" (regardless of whether the characters
are uppercase, lowercase, or some combination thereof) are reserved for use
by XML.

At this point, the query isn't returning any data when there's a namespace
specified in the data file, and XMLNamespaceManager doesn't like the
namespace when I attempt to add it.

Anyone know that the solution is?

Below is the code fragment and sample XML data.

Thanks,

Richard Rosenheim

code fragment:

Namespace = New Xml.XmlNamespaceManager(xmlDoc.NameTable)
Namespace.AddNamespace("xmlns",
"http://tempuri.org/MySchema.xsd")
Namespace.PushScope()
elem = xmlDoc.SelectSingleNode("/dsMyData/TestData", Namespace)

sample data file:

<?xml version="1.0" standalone="yes"?>
<dsMyData xmlns="http://tempuri.org/MyDataSchema.xsd">
<TestData>
<RecID>0</RecID>
<Title>Test</Title>
<URL>www.dummy.local</URL>
<UserName>john</UserName>
</TestData>
</dsMyData>

Nov 12 '05 #1
9 8997


Richard L Rosenheim wrote:

Namespace = New Xml.XmlNamespaceManager(xmlDoc.NameTable)
Namespace.AddNamespace("xmlns",
"http://tempuri.org/MySchema.xsd")
You need to declare a prefix for your namespace e.g.
Namespace.AddNamespace("choosenprefix",
"http://tempuri.org/MySchema.xsd")
Namespace.PushScope()
elem = xmlDoc.SelectSingleNode("/dsMyData/TestData", Namespace)
and then use that prefix in the XPath expression e.g.

xmlDoc.SelectSingleNode("/choosenprefix:dsMyData/choosenprefix:TestData",
Namespace)

<?xml version="1.0" standalone="yes"?>
<dsMyData xmlns="http://tempuri.org/MyDataSchema.xsd">
<TestData>


--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Hi,

http://tempuri.org/MySchema.xsd is a default namespace in your document,
so you should add it
to namespace manager as:

Namespace.AddNamespace(string.Empty, "http://tempuri.org/MySchema.xsd")

xmlns="namespace" syntax defines default namespace for the document rather
than
new prefix to namespace binding.

br,
Yuriy

I'm trying to query a XML file that was created via ADO.NET. My query
wasn't returning anything, and I tracked the problem to an issue with
the namespace that ADO.NET specified. When I remove the namespace
from the XML data, the query worked.

Okay, so I added code to create a XMLNamespaceManager and populate it
with the namespace. Now, I'm getting this exception:

Prefixes beginning with "xml" (regardless of whether the
characters are uppercase, lowercase, or some combination thereof) are
reserved for use by XML.

At this point, the query isn't returning any data when there's a
namespace specified in the data file, and XMLNamespaceManager doesn't
like the namespace when I attempt to add it.

Anyone know that the solution is?

Below is the code fragment and sample XML data.

Thanks,

Richard Rosenheim

code fragment:

Namespace = New Xml.XmlNamespaceManager(xmlDoc.NameTable)
Namespace.AddNamespace("xmlns",
"http://tempuri.org/MySchema.xsd")
Namespace.PushScope()
elem = xmlDoc.SelectSingleNode("/dsMyData/TestData",
Namespace)
sample data file:

<?xml version="1.0" standalone="yes"?>
<dsMyData xmlns="http://tempuri.org/MyDataSchema.xsd">
<TestData>
<RecID>0</RecID>
<Title>Test</Title>
<URL>www.dummy.local</URL>
<UserName>john</UserName>
</TestData>
</dsMyData>


Nov 12 '05 #3
Yuriy,

I tried adding the line you specified, but that didn't help. The query is
still not returning any nodes.

Thanks for the reply,

Richard Rosenheim
"Yuriy" <y.************@gmail.com> wrote in message
news:7c**************************@msnews.microsoft .com...
Hi,

http://tempuri.org/MySchema.xsd is a default namespace in your document,
so you should add it
to namespace manager as:

Namespace.AddNamespace(string.Empty, "http://tempuri.org/MySchema.xsd")

xmlns="namespace" syntax defines default namespace for the document rather
than
new prefix to namespace binding.

br,
Yuriy

I'm trying to query a XML file that was created via ADO.NET. My query
wasn't returning anything, and I tracked the problem to an issue with
the namespace that ADO.NET specified. When I remove the namespace
from the XML data, the query worked.

Okay, so I added code to create a XMLNamespaceManager and populate it
with the namespace. Now, I'm getting this exception:

Prefixes beginning with "xml" (regardless of whether the
characters are uppercase, lowercase, or some combination thereof) are
reserved for use by XML.

At this point, the query isn't returning any data when there's a
namespace specified in the data file, and XMLNamespaceManager doesn't
like the namespace when I attempt to add it.

Anyone know that the solution is?

Below is the code fragment and sample XML data.

Thanks,

Richard Rosenheim

code fragment:

Namespace = New Xml.XmlNamespaceManager(xmlDoc.NameTable)
Namespace.AddNamespace("xmlns",
"http://tempuri.org/MySchema.xsd")
Namespace.PushScope()
elem = xmlDoc.SelectSingleNode("/dsMyData/TestData",
Namespace)
sample data file:

<?xml version="1.0" standalone="yes"?>
<dsMyData xmlns="http://tempuri.org/MyDataSchema.xsd">
<TestData>
<RecID>0</RecID>
<Title>Test</Title>
<URL>www.dummy.local</URL>
<UserName>john</UserName>
</TestData>
</dsMyData>


Nov 12 '05 #4
Martin,

Thanks for replying.

The XML data file is being created by invoking the WriteXML method on an
ADO.NET dataset. It's ADO.NET that's coming up with the namespace and the
"xmlns" attribute. To the best of my knowledge, I don't have any control
over this aspect of the output. So, I am not aware of a way of changing it.
Actually, if I had my choice, I would prefer not to even have the namespace
specified at all.

Richard Rosenheim

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...


Richard L Rosenheim wrote:

Namespace = New Xml.XmlNamespaceManager(xmlDoc.NameTable)
Namespace.AddNamespace("xmlns",
"http://tempuri.org/MySchema.xsd")
You need to declare a prefix for your namespace e.g.
Namespace.AddNamespace("choosenprefix",
"http://tempuri.org/MySchema.xsd")
Namespace.PushScope()
elem = xmlDoc.SelectSingleNode("/dsMyData/TestData",

Namespace)
and then use that prefix in the XPath expression e.g.

xmlDoc.SelectSingleNode("/choosenprefix:dsMyData/choosenprefix:TestData",
Namespace)

<?xml version="1.0" standalone="yes"?>
<dsMyData xmlns="http://tempuri.org/MyDataSchema.xsd">
<TestData>


--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #5
"Richard L Rosenheim" <ri*****@rlr.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
I tried adding the line you specified, but that didn't help. The query is
still not returning any nodes.

"Yuriy" <y.************@gmail.com> wrote in message
news:7c**************************@msnews.microsoft .com...
Namespace.AddNamespace(string.Empty, "http://tempuri.org/MySchema.xsd") : : :
> Namespace.AddNamespace("xmlns",
> "http://tempuri.org/MySchema.xsd")
> Namespace.PushScope()
> elem = xmlDoc.SelectSingleNode("/dsMyData/TestData",
> Namespace)


When evaluating an XPath expression, even if the source document uses
the default namespace (without any prefix), the XPath expression must
have a prefix. Names in an XPath expression without a prefix are assumed
to be from the empty namespace URI, ''. (There is no way to specify an
empty string before a colon in the XPath expression.)

What you need to do Richard, is associate any prefix with the namespace
URI (it's always the namespace URI that counts -- the choice of prefix is
completely arbitrary).

// Make-up a namespace prefix, "ns1".
Namespace.AddNamespace( "ns1", "http://tempuri.org/MySchema.xsd")

// Change XPath expression to use this prefix and XmlNamespaceManager.
elem = xmlDoc.SelectSingleNode( "/ns1:dsMyData/ns1:TestData", Namespace)
Derek Harmon
Nov 12 '05 #6
"Richard L Rosenheim" <ri*****@rlr.com> wrote in message news:OV*************@TK2MSFTNGP12.phx.gbl...
ADO.NET dataset. It's ADO.NET that's coming up with the namespace and the
"xmlns" attribute. To the best of my knowledge, I don't have any control
over this aspect of the output. So, I am not aware of a way of changing it.
By default, the ADO.NET DataSet will use the default namespace for the
data (it just makes the XML serialization a little more compact). You can
make it use a namespace prefix of your own choosing by setting the Prefix
property of the DataSet before calling WriteXml( ).

dataSet1.Prefix = "ns1"

This will cause the XML serialized out of the DataSet to appear with an
xmlns declaration like this,

xmlns:ns1="http://tempuri.org/MySchema.xsd"
Actually, if I had my choice, I would prefer not to even have the namespace
specified at all.


For this purpose, you would (instead of setting Prefix) set the Namespace
property of the DataSet to the empty string,

dataSet1.Namespace = ""

This in turn leads to an XML serialization containing the xmlns declaration of,

xmlns:ns1=""

In this particular case, no XmlNamespaceManager is needed for your XPath
query.
Derek Harmon
Nov 12 '05 #7


Richard L Rosenheim wrote:

The XML data file is being created by invoking the WriteXML method on an
ADO.NET dataset. It's ADO.NET that's coming up with the namespace and the
"xmlns" attribute. To the best of my knowledge, I don't have any control
over this aspect of the output.
I haven't in any way suggested that you change the XML, but inside the
..NET code trying to read the XML you need to make sure you do as shown
below:

"Martin Honnen" <ma*******@yahoo.de> wrote
You need to declare a prefix for your namespace e.g.
Namespace.AddNamespace("choosenprefix",
"http://tempuri.org/MySchema.xsd")

and then use that prefix in the XPath expression e.g.

xmlDoc.SelectSingleNode("/choosenprefix:dsMyData/choosenprefix:TestData",
Namespace)


An XPath expression needs to use a prefix to refer to nodes in a
namespace so in the .NET code you need a prefix you can choose,
independent of what your XML file uses (default namespace or prefix).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #8
Derek,

Thank you. As you said, adding a namespace with a completely arbitrary
prefix resolved my problem.

And thank you for the other post explaining how to specify namespaces to
ADO.NET.

Richard Rosenheim
"Derek Harmon" <lo*******@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Richard L Rosenheim" <ri*****@rlr.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
I tried adding the line you specified, but that didn't help. The query is
still not returning any nodes.

"Yuriy" <y.************@gmail.com> wrote in message
news:7c**************************@msnews.microsoft .com...
Namespace.AddNamespace(string.Empty, "http://tempuri.org/MySchema.xsd")

: : : > Namespace.AddNamespace("xmlns",
> "http://tempuri.org/MySchema.xsd")
> Namespace.PushScope()
> elem = xmlDoc.SelectSingleNode("/dsMyData/TestData",
> Namespace)


When evaluating an XPath expression, even if the source document uses
the default namespace (without any prefix), the XPath expression must
have a prefix. Names in an XPath expression without a prefix are assumed
to be from the empty namespace URI, ''. (There is no way to specify an
empty string before a colon in the XPath expression.)

What you need to do Richard, is associate any prefix with the namespace
URI (it's always the namespace URI that counts -- the choice of prefix is
completely arbitrary).

// Make-up a namespace prefix, "ns1".
Namespace.AddNamespace( "ns1", "http://tempuri.org/MySchema.xsd")

// Change XPath expression to use this prefix and XmlNamespaceManager.
elem = xmlDoc.SelectSingleNode( "/ns1:dsMyData/ns1:TestData",

Namespace)

Derek Harmon

Nov 12 '05 #9
Thank you. Between you and the postings from Derek, I got everything
working.

Richard Rosenheim
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...


Richard L Rosenheim wrote:

The XML data file is being created by invoking the WriteXML method on an
ADO.NET dataset. It's ADO.NET that's coming up with the namespace and the "xmlns" attribute. To the best of my knowledge, I don't have any control over this aspect of the output.


I haven't in any way suggested that you change the XML, but inside the
.NET code trying to read the XML you need to make sure you do as shown
below:

"Martin Honnen" <ma*******@yahoo.de> wrote
>
You need to declare a prefix for your namespace e.g.
Namespace.AddNamespace("choosenprefix",
"http://tempuri.org/MySchema.xsd")

and then use that prefix in the XPath expression e.g.

xmlDoc.SelectSingleNode("/choosenprefix:dsMyData/choosenprefix:TestData",
Namespace)


An XPath expression needs to use a prefix to refer to nodes in a
namespace so in the .NET code you need a prefix you can choose,
independent of what your XML file uses (default namespace or prefix).
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #10

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

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
3
by: Oisin Grehan | last post by:
Hi, I can't for the life of me get this to work properly. I've searched for examples, but none of the examples quite match my environment. Here is my XML (please treat this as immutable -- no...
1
by: Dan via .NET 247 | last post by:
Hi All, Is it possible to serialize an instance of XmlNamespaceManager? I'd like to use it to manage a collection of namespaces and savethat collection for later use but when attempting to...
1
by: Victor Hadianto | last post by:
Hi All, I have the following code: <snip> XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""?> <myConfig:myConfig...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
0
by: Timo | last post by:
If there's a better place to ask, please advise. I've serialized a custom collection class using the XMLSerializer, and now I'd like to deserialize it. The xml representation of the serialized...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
1
by: peterloh | last post by:
Hi, I'm trying to read a simple Word 2007 document with the contents of "This is a test." Could someone please enlighten me about why the following code doesn't work... Dim strXMLDocument...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.