468,132 Members | 1,585 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,132 developers. It's quick & easy.

How to read "xmlns" from XML

ano
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
Aug 11 '06 #1
3 8727


ano wrote:

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/
Aug 12 '06 #2
ano
Thanks!!, it's work.

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

thanks,
ano

"Martin Honnen" wrote:
>

ano wrote:

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/
Aug 12 '06 #3


ano wrote:

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/
Aug 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Dickens | last post: by
3 posts views Thread by Keith Hill | last post: by
5 posts views Thread by NeilL | last post: by
reply views Thread by R. Ian Lee | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.