all groups > dotnet xml > august 2003 >
You're in the

dotnet xml

group:

NameSpace SelectNodes Problem


NameSpace SelectNodes Problem Alexander Gnauck
8/23/2003 7:29:00 PM
dotnet xml:
Hello,

i have problems with the Namespaces and the .Net XML Parser
My XML looks like this:

<query xmlns="jabber:iq:roster">
<item jid="srlee@localhost" name="srlee"
subscription="both"><group>contacts</group></item>
<item jid="tlee@localhost" name="Troy"
subscription="both"><group>contacts</group></item>
<item jid="bglee@localhost" name="Barry"
subscription="both"><group>contacts</group></item>
<item jid="dslee@localhost" name="Debbie"
subscription="both"><group>contacts</group></item>
</query>

Now i select nodes with SelectNodes. Sometimes i change or add some
attributes to the selected node. The XML is located in a normal
XmlDocument Object (doc).

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("foo", "jabber:iq:roster" );
XmlNode nItem = doc.SelectSingleNode(
"//foo:item[@jid='srlee@localhost']", nsmgr );

Now when i look at the OuterXml of the selected node i get the
following:

<item jid="srlee@localhost" name="srlee" subscription="both"
xmlns="jabber:iq:roster"><group
xmlns="jabber:iq:roster">contacts</group></item>

The parser adds the Namespace to every tag. Thats very very bad for my
applciation. I wanna have the raw XM here. Is there a way to get this
node without the added namespaces? In the old COM MSXML 2-4 parsers i
never had this problems. Is there a way to tell the parser to handle the
namespaces like normal attribbutes? Or to ignore them? They make me only
trouble. Has anybody a idea or similar problems ?

Thanx Alex

--
AG Software
Alexander Gnauck
Wiesental 3
74182 Obersulm
gnauck(at)ag-software.de
http://www.ag-software.de

Re: NameSpace SelectNodes Problem Christoph Schittko [MVP]
8/23/2003 10:00:13 PM
Alexander ,

The namespace references are not "added". They are a vital part of the
infoset of the document from which you select. They are a part a of the
nodes you select, without them they would be different elements.

Now what exactly do you want? Do you really want elements that do no belong
to any namespace or do you not want the namespace repeated on every element
you selected?

If you want to strip the namespace definitions after you selected the nodes,
you have two options. First, you can create a new document, iterate over the
nodes you selected and and add nodes to the new document with the same names
and content as the ones you selected, but without the namespaces. Second,
you can do this via XSLT ins tead of selecting the nodes and iterating over
the results. Once you went through this step then the nodes in the newly
created doc do not belong to any namespace.

If you just don't want the namespace definition on every node then you need
have parent node that contains the selected nodes. The parent can declare
the namespace as the default namespace or define a prefix that will be used
to indicate the namespace in which the elements are defined.

Did this help clear thing up? If you could post what you are actually trying
to accomplish and why you don't want the namespace definitions maybe we
could give more detailed advice.

HTH,
Christoph Schittko
Software Architect, .NET Mentor
MS MVP XML .NET


[quoted text, click to view]

Re: NameSpace SelectNodes Problem Alexander Gnauck
8/24/2003 12:02:58 PM
Christoph,


[quoted text, click to view]

i dont want the namespace repeated on all the elements i selected.
Because normally i have only one namespace in my documents. And i always
know to wich namespace they belong.

[quoted text, click to view]

yes i think thats the only solution for now in my case.

[quoted text, click to view]

yes this helped me. i try to add a parent node with a namespace. I think
that will be a workaround for now. I will post my results then.


Alex
Re: NameSpace SelectNodes Problem Alexander Gnauck
8/26/2003 1:10:35 PM
Hello Christoph,

[quoted text, click to view]

i tried this now with the parent node. Its crazy, sopmetimes it works
and sometimes not. When i import a node from another document then it
doesnt work.

For Example:
Importnode.OuterXml = "<import a1="test1" a2="test2" xmlns="mynamespace"/>"

thats the node i selected with SelectNodes. Wanna get rid of the
namespace and must import it to another document. So i create a new
XmlDocument with a parent node and the same namespace.
XmlDocument looks like this:
<test>
<parent xmlns="mynamespace"/>
</test>

After ImportNode my document looks like his :(

<test>
<parent xmlns="mynamespace">
<import a1="test1" a2="test2" xmlns="mynamespace"/>
</parent>
</test>

so the NameSpace in the import node is still there. What am i doing wrong ?

Thanx Alex
AddThis Social Bookmark Button