Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 11th, 2006, 07:55 PM
ano
Guest
 
Posts: n/a
Default How to read "xmlns" from XML

Hi,

Anyone knows how to get "xmlns" value from XML file?
For example, how to check that this xml file has a xmlns or not?
Or how to read the xmlns value?

<bookstore xmlns:bk="http://www.lucernepublishing.com">
<book>
<title>Pride And Prejudice</title>
</book>
</bookstore>

thanks,
ano
  #2  
Old August 12th, 2006, 03:45 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: How to read "xmlns" from XML



ano wrote:

Quote:
Anyone knows how to get "xmlns" value from XML file?
The XmlReader will show them as attribute nodes in the predefined
namespace http://www.w3.org/2000/xmlns/ so a C#/NET 2.0 snippet like this

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
while (xmlReader.MoveToNextAttribute()) {
if (xmlReader.NamespaceURI ==
"http://www.w3.org/2000/xmlns/") {
Console.WriteLine("Found namespace declaration
{0}=\"{1}\".", xmlReader.Name, xmlReader.Value);
}
}
}
}
}

will read through the complete XML document and output all namespace
declarations found. For C#/NET 1.x simply use e.g. new
XmlTextReader(@"file.xml") instead of XmlReader.Create(@"file.xml").

If you want to use another API then tell us which one exactly and we can
tell you how to look for namespace declarations (hint: in the XPath data
model namespace declarations are _not_ attributes so there you need to
seearch the namespace axis).


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old August 12th, 2006, 04:45 PM
ano
Guest
 
Posts: n/a
Default Re: How to read "xmlns" from XML

Thanks!!, it's work.

Is it possible to look for namespace by using XPathDocument or XPathNavigator?

thanks,
ano

"Martin Honnen" wrote:
Quote:
>
>
ano wrote:
>
>
Quote:
Anyone knows how to get "xmlns" value from XML file?
>
The XmlReader will show them as attribute nodes in the predefined
namespace http://www.w3.org/2000/xmlns/ so a C#/NET 2.0 snippet like this
>
using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
while (xmlReader.MoveToNextAttribute()) {
if (xmlReader.NamespaceURI ==
"http://www.w3.org/2000/xmlns/") {
Console.WriteLine("Found namespace declaration
{0}=\"{1}\".", xmlReader.Name, xmlReader.Value);
}
}
}
}
}
>
will read through the complete XML document and output all namespace
declarations found. For C#/NET 1.x simply use e.g. new
XmlTextReader(@"file.xml") instead of XmlReader.Create(@"file.xml").
>
If you want to use another API then tell us which one exactly and we can
tell you how to look for namespace declarations (hint: in the XPath data
model namespace declarations are _not_ attributes so there you need to
seearch the namespace axis).
>
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
>
  #4  
Old August 12th, 2006, 05:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: How to read "xmlns" from XML



ano wrote:

Quote:
Is it possible to look for namespace by using XPathDocument or XPathNavigator?

Yes, in the XPath data model any node has associated namespace nodes
which you can look at with an XPath expression using the XPath namespace
axis e.g.

XPathDocument xPathDocument = new XPathDocument(@"file.xml");
XPathNavigator navigator = xPathDocument.CreateNavigator();
XPathNodeIterator nodeIterator =
navigator.Select(@"//namespace::*[not(. = ../../namespace::*)]");
while (nodeIterator.MoveNext()) {
Console.WriteLine(
"Found namespace node with prefix \"{0}\" and value \"{1}\".",
nodeIterator.Current.LocalName, nodeIterator.Current.Value);
}

Note that you will always find a namespace node for prefix "xml" and URI
"http://www.w3.org/XML/1998/namespace" on the namespace axis for the
predefined xml namespace (e.g. for xml:lang or xml:space attributes)
although this is not defined in the XML markup.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles