Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : XMLNamespaceManager problem


Richard L Rosenheim
2/11/2005 10:01:18 PM
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>


Yuriy
2/12/2005 4:38:59 AM
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


[quoted text, click to view]


Martin Honnen
2/12/2005 1:33:04 PM


[quoted text, click to view]


[quoted text, click to view]

You need to declare a prefix for your namespace e.g.
Namespace.AddNamespace("choosenprefix",
"http://tempuri.org/MySchema.xsd")

[quoted text, click to view]

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

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


[quoted text, click to view]



--

Martin Honnen
Richard L Rosenheim
2/12/2005 6:22:13 PM
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


[quoted text, click to view]

Richard L Rosenheim
2/12/2005 6:27:55 PM
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



[quoted text, click to view]

Derek Harmon
2/12/2005 10:15:55 PM
[quoted text, click to view]

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

Derek Harmon
2/12/2005 10:21:47 PM
[quoted text, click to view]

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"

[quoted text, click to view]

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

Martin Honnen
2/13/2005 3:17:43 PM


[quoted text, click to view]


[quoted text, click to view]

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:


[quoted text, click to view]

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
Richard L Rosenheim
2/13/2005 3:54:11 PM
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


[quoted text, click to view]

Richard L Rosenheim
2/13/2005 3:55:08 PM
Thank you. Between you and the postings from Derek, I got everything
working.

Richard Rosenheim


[quoted text, click to view]

AddThis Social Bookmark Button