473,782 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lucernepubl ishing.com">
<book>
<title>Pride And Prejudice</title>
</book>
</bookstore>

thanks,
ano
Aug 11 '06 #1
3 9910


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.Creat e(@"file.xml") ) {
while (xmlReader.Read ()) {
if (xmlReader.Node Type == XmlNodeType.Ele ment) {
while (xmlReader.Move ToNextAttribute ()) {
if (xmlReader.Name spaceURI ==
"http://www.w3.org/2000/xmlns/") {
Console.WriteLi ne("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.Creat e(@"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.Creat e(@"file.xml") ) {
while (xmlReader.Read ()) {
if (xmlReader.Node Type == XmlNodeType.Ele ment) {
while (xmlReader.Move ToNextAttribute ()) {
if (xmlReader.Name spaceURI ==
"http://www.w3.org/2000/xmlns/") {
Console.WriteLi ne("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.Creat e(@"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.C reateNavigator( );
XPathNodeIterat or nodeIterator =
navigator.Selec t(@"//namespace::*[not(. = ../../namespace::*)]");
while (nodeIterator.M oveNext()) {
Console.WriteLi ne(
"Found namespace node with prefix \"{0}\" and value \"{1}\".",
nodeIterator.Cu rrent.LocalName , nodeIterator.Cu rrent.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
10043
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" xmlns:ns1="abc" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" media-type="text/xml" standalone="yes" version="1.0"/> <xsl:template match="/">
3
7144
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 sub-elements are created without any namespaces like so: XmlElement elem = doc.CreateElement("Foo"); root.AppendChild(elem); but in the output I get this:
5
12443
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 resulting XML file has the following <Author xmlns="">something</Author>
3
3331
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 class , I can set the XmlSchemaForm.None or Qualified so that there will no be a 'xmlns=""' in the following namespace, but I don't know how to do that in WebService plumbing. this null namespace will makes my web service wrapper class complains....
0
1596
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 xmlns="" attributes into my <Worksheetelements which causes the worksheet not to work with Excel. I'm using the System.Xml.Xsl.XslTransform class to perform the transform. Here's a snippet of the XSLT template that is called: <xsl:template...
0
1438
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> <item>some more stuff here</item> <item>even more stuff here</item> </elem3>
3
2880
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 version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="aproductsearch.xslt"?> <ProductSearch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:aprods" xsi:schemaLocation="urn:yahoo:aprods...
4
4821
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 don't really understand how these namespace work in xml. (I am however aware of what problems namespaces solve) I'm not even sure if the provided xsd is 'common' practice, although it validates correctly. So I'll describe exactly what I've...
4
17885
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, but if I leave it in there are issues reading/saving the file. Is there any way to remove this xmlns="" in the xslt? Cheers,
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.