472,991 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 software developers and data experts.

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 9815


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
3
by: Keith Hill | last post by:
I am creating an XmlDocument in code and then using XmlTextWriter via doc.WriteTo(xwriter) to output the result to a text box. I have a root element that defines a default namespace. However, the...
5
by: NeilL | last post by:
In the XML document I'm trying to create I do the following elem = _doc.CreateElement("Author"); elem.InnerText = "something"; parentElem.AppendChild(elem); Thiw works properly however the...
3
by: Jim Hsu | last post by:
when I use the XmlWebSerivce to response the xmlelement to Web Service client. the ASP.net plumbing work ( the XmlSerializer in WebServices ) will serialize the XML if we can control the wrapper...
0
by: R. Ian Lee | last post by:
I've built an XSLT file that transforms data to SpreadsheetML format. The XSLT uses a <xsl:call-template/to build each worksheet. For some reason, when I transform the file, it is inserting...
0
by: SolaFide | last post by:
I have an XML doc that looks something like this: <?xml version="1.0" encoding="utf-8"?> <elem1> <elem2 xmlns="http://someurl.com"> <elem3> <item>some stuff here</item> ...
3
by: Lee | last post by:
Hello guys, I am new to XML and working on a XSLT to transforn yahoo shopping search result to html. my problem is the return XML contain xmlns in root element, here is the sample xml: <?xml...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
4
markmcgookin
by: markmcgookin | last post by:
Hi, I am creating an MXL doc using XSLT but for some reason it is churning out elements like this <DateTimeLastSaved xmlns="" /> <UserName xmlns="" /> when I delete xmlns="" it works fine,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.