I have an XML data set coming into my C# app using the following:
var _xml = XElement.Load("http://myserver:8080/myxmlfile.xml").Elements("ns:results");
This works great as long as there is no namespace specified in the xml data coming in i.e. ...Element("results"). But if there is a namespace specified in the xml data coming in i.e. "ns" (see above) I get an error i.e. the ":" character is not recognized.
So, it seems using the above the namespace is not recognized and it bombs.
I've looked at namespacemanager examples but they all seem to talk about reading a "table". All I need is for the above XElement load to recognize the namespace and go about it's business as usual.
I've tried:
XmlNamespaceManager nsmanager = new XmlNamespaceManager("http://myserver:8080)/incomingxmlfile.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager("http://myserver:8080)/incomingxmlfile.xml");
XmlNamespaceManager needs a parameter that XmlDataTable。So you cannot assign a string value to it——you have to use something like XmlTextReader to assign the NameTable as the value of XmlDataTable:
And there's something wrong with your url——"http://myserver:8080)/incomingxmlfile.xml"——Notice you've got a ")";and maybe it is:
http://myserver:8080/incomingxmlfile.xml
【Sample From MSDN】
XmlTextReader reader = new XmlTextReader("myfile.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");
nsmanager.PushScope();
nsmanager.AddNamespace("msstore", "www.microsoft.com/store");
while (reader.Read())
{
Console.WriteLine("Reader Prefix:{0}", reader.Prefix);
Console.WriteLine("XmlNamespaceManager Prefix:{0}",
nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)));
}
【xml】
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
【cs codes】
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");
//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList){
Console.WriteLine(isbn.Value);
}
}
}
Thanks for the reply. I tried what was suggested but it turns out that my xml data that is coming in is the result of an xquery from a server database and not an xml file on a file system i.e. xmlfile.xml. When I tried the above (i.e. I made it like it was
an xml document coming in) I got an error that said, "Data at the root level is invalid. Line 1, position 1". Of coure there is no utf info like in a valid or well formed xml file.
My http call was like the above but the xml data set returned had namespace like this:
<map:results xmlns:ns="http://something.com/map">
<map:facet name="genre">
<map:facet name="genre">
<map:result>
<map:value>Pop</map:value>
<map:inQuery>false</map:inQuery>
<map:count>248</map:count>
.
.
.
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements("map:results")
So, it seems I can't get .net or LINQ to recognize namespaces if it is not an .xml file read from disk.
Does this mean .net can't handle namespaces that are part of an xml stream like the data example I gave above?
What to do? Could so something goofy like build an xml file from the xml stream, write it to disk, then read it back in just so the namespace problem can be worked around.
sorry for any kind of self-defined namespace,you CANNOT do directly like what you did to a general common tag name……;What you can do is just use XNameSpace.Get("Your xmlns's address) plus the suffix——
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements(XNameSpace.Get("xmlns's Address of map")+"results")
randy4921
0 Points
5 Posts
Help with Namespaces and XElement.Load
May 09, 2012 04:46 PM|LINK
I have an XML data set coming into my C# app using the following:
var _xml = XElement.Load("http://myserver:8080/myxmlfile.xml").Elements("ns:results");
This works great as long as there is no namespace specified in the xml data coming in i.e. ...Element("results"). But if there is a namespace specified in the xml data coming in i.e. "ns" (see above) I get an error i.e. the ":" character is not recognized. So, it seems using the above the namespace is not recognized and it bombs.
I've looked at namespacemanager examples but they all seem to talk about reading a "table". All I need is for the above XElement load to recognize the namespace and go about it's business as usual.
I've tried:
XmlNamespaceManager nsmanager = new XmlNamespaceManager("http://myserver:8080)/incomingxmlfile.xml");
nsmanager.AddNamespace("ns", "http://logic.com/somenamespacename")
But this format is not recognized.
Thanks for any help!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with Namespaces and XElement.Load
May 11, 2012 01:29 AM|LINK
XmlNamespaceManager needs a parameter that XmlDataTable。So you cannot assign a string value to it——you have to use something like XmlTextReader to assign the NameTable as the value of XmlDataTable:
And there's something wrong with your url——"http://myserver:8080)/incomingxmlfile.xml"——Notice you've got a ")";and maybe it is:
http://myserver:8080/incomingxmlfile.xml
【Sample From MSDN】
XmlTextReader reader = new XmlTextReader("myfile.xml"); XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable); nsmanager.AddNamespace("msbooks", "www.microsoft.com/books"); nsmanager.PushScope(); nsmanager.AddNamespace("msstore", "www.microsoft.com/store"); while (reader.Read()) { Console.WriteLine("Reader Prefix:{0}", reader.Prefix); Console.WriteLine("XmlNamespaceManager Prefix:{0}", nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI))); }randy4921
0 Points
5 Posts
Re: Help with Namespaces and XElement.Load
Jun 07, 2012 07:51 PM|LINK
Sorry about the typo in the url.
Also, sorry I don't have very much experience with LINQ.
I had success with LINQ when I was receiving a string of xml results WITH NO NAMESPACE from of an xquery via http. Like this:
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements("results")
But when I had another xml data set WITH NAMWSPACES it bombed with an error like "the character ":" is not recognized".
My http call was like the above but the xml data set returned had namespace like this:
<ns:results xmlns:ns="http://something.com/ns">
<map:facet name="genre">
<map:facet name="genre">
<map:result>
<map:value>Pop</map:value>
<map:inQuery>false</map:inQuery>
<map:count>248</map:count>
.
.
.
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements("map:results")
So, in this context is there a way to tell LINQ that namespaces are present, read them and return them just like before?
Thanks for any help!
randy
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with Namespaces and XElement.Load
Jun 08, 2012 01:14 AM|LINK
Sorry but you should use
to load the defined namespaces first,and then you can use:XmlDocument's SelectNodes("Your Node Path",nsmanager);【xml】 <?xml version="1.0"?> <!-- A fragment of a book store inventory database --> <bookstore xmlns:bk="urn:samples"> <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8"> <title>Pride And Prejudice</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>24.95</price> </book> <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1"> <title>The Handmaid's Tale</title> <author> <first-name>Margaret</first-name> <last-name>Atwood</last-name> </author> <price>29.95</price> </book> <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6"> <title>Emma</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3"> <title>Sense and Sensibility</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> </bookstore> 【cs codes】 using System; using System.IO; using System.Xml; public class Sample { public static void Main() { XmlDocument doc = new XmlDocument(); doc.Load("booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces. XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("bk", "urn:samples"); //Select and display the value of all the ISBN attributes. XmlNodeList nodeList; XmlElement root = doc.DocumentElement; nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr); foreach (XmlNode isbn in nodeList){ Console.WriteLine(isbn.Value); } } }randy4921
0 Points
5 Posts
Re: Help with Namespaces and XElement.Load
Jun 15, 2012 04:35 PM|LINK
Thanks for the reply. I tried what was suggested but it turns out that my xml data that is coming in is the result of an xquery from a server database and not an xml file on a file system i.e. xmlfile.xml. When I tried the above (i.e. I made it like it was an xml document coming in) I got an error that said, "Data at the root level is invalid. Line 1, position 1". Of coure there is no utf info like in a valid or well formed xml file.
My http call was like the above but the xml data set returned had namespace like this:
<map:results xmlns:ns="http://something.com/map">
<map:facet name="genre">
<map:facet name="genre">
<map:result>
<map:value>Pop</map:value>
<map:inQuery>false</map:inQuery>
<map:count>248</map:count>
.
.
.
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements("map:results")
So, it seems I can't get .net or LINQ to recognize namespaces if it is not an .xml file read from disk.
Does this mean .net can't handle namespaces that are part of an xml stream like the data example I gave above?
What to do? Could so something goofy like build an xml file from the xml stream, write it to disk, then read it back in just so the namespace problem can be worked around.
ugh! Any help appreciated.
Randy
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with Namespaces and XElement.Load
Jun 16, 2012 02:18 AM|LINK
sorry for any kind of self-defined namespace,you CANNOT do directly like what you did to a general common tag name……;What you can do is just use XNameSpace.Get("Your xmlns's address) plus the suffix——
var _xml = XElement.Load("http//server1:8080/index.xqy").Elements(XNameSpace.Get("xmlns's Address of map")+"results")
randy4921
0 Points
5 Posts
Re: Help with Namespaces and XElement.Load
Jun 18, 2012 06:40 PM|LINK
Thanks much for the reply.
When I saw your suggestion I thought for sure it would work. However, I'm still gettin a "null" data set returned.
This is what I did:
var _xmltemp = XElement.Load("http//server1:8080/index.xqy")
In the Visual Studio debugger when I veiw the xml data from the source it looks just like I expect.
This is it:
<map:results xmlns:map="http://something.com/map">
<map:facet name="genre">
<map:facet name="genre">
<map:result>
<map:value>Pop</map:value>
<map:inQuery>false</map:inQuery>
<map:count>248</map:count>
. etc....
But when I get to the next statement in the debugger which is:
var _xml =_xmltemp.Elements(XNameSpace.Get("http://something.com/map")+"results")
the variable _xml is null.
Does this look correct? Did I miss something?
Again, thanks for the reply. I thought you had fixed my problem and maybe you have but I have some error.
Randy
randy4921
0 Points
5 Posts
Re: Help with Namespaces and XElement.Load
Jun 18, 2012 11:18 PM|LINK
All,
Looks like the results ARE coming back. The debugger was showing null but data was really there.
Thanks much for the help.
I think this is resolved.
Randy