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> 9 8926
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/
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>
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>
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/
"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
"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
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/
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
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/ This discussion thread is closed Replies have been disabled for this discussion. Similar topics
117 posts
views
Thread by Peter Olcott |
last post: by
|
3 posts
views
Thread by Oisin Grehan |
last post: by
|
1 post
views
Thread by Dan via .NET 247 |
last post: by
|
1 post
views
Thread by Victor Hadianto |
last post: by
|
28 posts
views
Thread by Jon Davis |
last post: by
|
6 posts
views
Thread by Ammar |
last post: by
|
reply
views
Thread by Timo |
last post: by
|
16 posts
views
Thread by Dany |
last post: by
|
1 post
views
Thread by peterloh |
last post: by
| | | | | | | | | | |