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>
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] > 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 [quoted text, click to view] "Yuriy" <y.dot.solodkyy@gmail.com> wrote in message news:7c6aeb04511758c6df27b8ad7c6d@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 [quoted text, click to view] "Martin Honnen" <mahotrash@yahoo.de> wrote in message news:elZ0E8PEFHA.2540@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/
[quoted text, click to view] "Richard L Rosenheim" <richard@rlr.com> wrote in message news:%23KUtVLXEFHA.3976@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.dot.solodkyy@gmail.com> wrote in message > news:7c6aeb04511758c6df27b8ad7c6d@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
[quoted text, click to view] "Richard L Rosenheim" <richard@rlr.com> wrote in message news:OVXmhOXEFHA.936@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" [quoted text, click to view] > 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
[quoted text, click to view] Richard L Rosenheim wrote:
[quoted text, click to view] > 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: [quoted text, click to view] > "Martin Honnen" <mahotrash@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
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] "Derek Harmon" <loresayer@msn.com> wrote in message news:%23A8j3oXEFHA.3992@tk2msftngp13.phx.gbl... > "Richard L Rosenheim" <richard@rlr.com> wrote in message news:%23KUtVLXEFHA.3976@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.dot.solodkyy@gmail.com> wrote in message > > news:7c6aeb04511758c6df27b8ad7c6d@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 [quoted text, click to view] "Martin Honnen" <mahotrash@yahoo.de> wrote in message news:%23LsbKbdEFHA.560@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" <mahotrash@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/
Don't see what you're looking for? Try a search.
|