473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlDocument.Loa dXml and namespaces

Hi;

Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?

And if you can't get it from the DOM, any other way to get the namespace(s)?
I'm assuming doing a SAX read which returns each element including NameSpaces
and pull it form that (and build up the DOM at the same time)?

In my case I get a Stream so I can't read it twice as the Stream may not
support reset. And it may have NS elements - but I don't know if there are
any or what they are except by reading the Stream.

--
thanks - dave
Nov 12 '05 #1
11 4346
David Thielen wrote:
Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?


Of course not. You can select namespace declarations using
doc.SelectNodes ("//namespace::*").

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
Hi;

This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn't get just 3?

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?


Of course not. You can select namespace declarations using
doc.SelectNodes ("//namespace::*").

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #3
For the xml:
<root xmlns="http://www.test.org" xmlns:sns="http ://www.test.org/sub"
xmlns:mns="http ://www.test.org/mini">
<data>
<items>
<item id="1">
<sns:subItem sid="11">dave</sns:subItem>
<sns:subItem sid="12">thiele n</sns:subItem>
</item>
<item id="2">
<sns:subItem sid="21">shirle y</sns:subItem>
</item>
</items>
<mns:more>dav e thielen</mns:more>
</data>
</root>

The nodes returned are:
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace

??? - thanks - dave
Nov 12 '05 #4


David Thielen wrote:
This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn't get just 3?


Because in the XPath data model any element has in scope namespace nodes
and that way if you for instance declare namespaces on the root element
and do not declare/redeclare namespaces on any child elements then
besides the root element all child elements also have those namespaces
in scope as nodes of their own.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #5
David Thielen wrote:
This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn't get just 3?


Select unique ones (and omit default "xml" namespace):
//namespace::*[name() != 'xml'][not(../../namespace::*=.)]

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #6
Oh right, that makes a lot of sense -- thanks - dave
"Martin Honnen" wrote:


David Thielen wrote:
This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn't get just 3?


Because in the XPath data model any element has in scope namespace nodes
and that way if you for instance declare namespaces on the root element
and do not declare/redeclare namespaces on any child elements then
besides the root element all child elements also have those namespaces
in scope as nodes of their own.

--

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

Nov 12 '05 #7
Hi;

This worked great but I don't understand it totally. I get the [name() !=
'xml']

But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
works.

Also, when you do xpath[a][b] is that saying both a and b must be true? Can
you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
as the same thing? Or am I not understanding this?

I'm asking the above questions because I just want to understand xpath better.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn't get just 3?


Select unique ones (and omit default "xml" namespace):
//namespace::*[name() != 'xml'][not(../../namespace::*=.)]

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #8
David Thielen wrote:

But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
works.
In XPath data model namespace nodes are propagated/copied down the tree
- each node in a subtree has namespace node for each namespace in context.
This predicate checks that parent of the element bearing the namespace
node doesn't have such namespace node. Effectively that allows to select
only namespace nodes on elements where namespace is declared.

Also, when you do xpath[a][b] is that saying both a and b must be true?
Yes.
Can
you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
as the same thing? Or am I not understanding this?


Yes, that's the same.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #9
Hi;

Ok, that makes sense. To make sure I understand this:
.../../namespace::* is getting the grandparent of the node on (../../) and
then getting any nodes in that element that start with namespace:: - why not
.../namespace::* - isn't that the parent?

And then the =. is saying equal to the node presently on . being the node on.

Is that correct?

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:

But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
works.


In XPath data model namespace nodes are propagated/copied down the tree
- each node in a subtree has namespace node for each namespace in context.
This predicate checks that parent of the element bearing the namespace
node doesn't have such namespace node. Effectively that allows to select
only namespace nodes on elements where namespace is declared.

Also, when you do xpath[a][b] is that saying both a and b must be true?


Yes.
Can
you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
as the same thing? Or am I not understanding this?


Yes, that's the same.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7359
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents written to the output stream. We are willing to pay $ to any expert (e.g. MVP) consultant who has to time to help us track down this problem. (we will discuss rates if you can prove your expertise, and problem solving approach).
1
17140
by: Martin Honnen | last post by:
With both .NET 1.0 and 1.1 I have found the following strange behaviour where System.Xml.XmlDocument.LoadXml doesn't throw an error when parsing a text node with a character reference to an invalid characters like &#x1;. Using the CreateTextNode method I create a text node containing "\u0001a" (C# string literal notation). As far as I understand the DOM allows that and an implementation is not required to throw an error. When OuterXml is...
4
4848
by: Robert Rossney | last post by:
I'm trying to send and receive XmlDocument objects using the System.Messaging.dll functions. The code I've written follows, as best I can tell, the methodology used in the sample code for the XmlMessageFormatter class. But it isn't working -- specifically, receiving isn't working. What am I doing wrong here? I begin by creating a MessageQueue object and setting up its XmlMessageFormatter to handle XmlDocument objects: System.Type...
2
8693
by: Eric Zechman via .NET 247 | last post by:
I have a project in which I get xml posted via a hidden form field (data=<xml></xml>). People are entering names like Bill & Sandy Smith. this is coming over as: <NameInfo><FamilyName><FamilyNames>David &amp; Karen</FamilyNames><Surname>Adams</Surname></FamilyName></NameInfo> When I try to load this up in a XmlDocument it blows up. string xmlData = Request.Form; dom.LoadXml(xmlData); results with this: "This is an unexpected token. The...
1
3031
by: AJ Brown | last post by:
I'm loading an XmlDocument object from a string using LoadXml. The string is a well-formed XML fragment (see example) and loads fine, however there are entities (&amp;) within attribute values. The loader parses the incoming entities and converts them to their character references automatically. How would I retain the entity? Example incoming fragment: <company name="B &amp; B Electric" />
2
6211
by: Dave | last post by:
Hi, Is there an easier way to pull a subset of nodes from one XmlDocument to another? I have the code below but would like to know if there is a more streamlined method. Thanks, Dave XmlNodeList nodeList = doc1.SelectNodes("//row"); foreach (XmlNode nodeCode in nodeList) { sb.Append(nodeCode.OuterXml);
19
16225
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org" xmlns:sns="http://www.test.org/sub" xmlns:mns="http://www.test.org/mini"> <data>
2
2762
by: Lupina | last post by:
Hi I want load whole xml file, I try do it in the same way as I did it in windows application. try { XmlDocument myDoc = new XmlDocument();
4
1817
by: Carlos Albert | last post by:
Hello, Would you tell me if there is a way to extract a single node as a new xmldocument? Thanks.
0
9680
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
9528
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
10455
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
10228
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
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
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...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
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
3
2925
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.