473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

NameSpace SelectNodes Problem

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
Nov 11 '05 #1
5 7360
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
"Alexander Gnauck" <gn****@ag-software.de> wrote in message
news:bi*************@news.t-online.com...
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

Nov 11 '05 #2
Christoph,

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?
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.
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.
yes i think thats the only solution for now in my case.
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.


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

Nov 11 '05 #3
Hello Christoph,
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.


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

Nov 11 '05 #4
Hello Oleg,

Oleg Tkachenko wrote:
Alexander Gnauck wrote:
thats the node i selected with SelectNodes. Wanna get rid of the
namespace and must import it to another document.


Namespace is an integral part of node and cannot be got rid of. The only
way is to recreate node with no namespace.
Speaking of your original question - I can't reproduce the problem, the
following piece of code

static void Main(string[] args) {
XmlDocument doc = new XmlDocument();
string xml =
@"<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>";
doc.LoadXml(xml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("foo", "jabber:iq:roster" );
XmlNode nItem = doc.SelectSingleNode(
"//foo:item[@jid='srlee@localhost']", nsmgr );
Console.WriteLine(nItem.OuterXml);
}

gives me

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


Your Output also has the namespace jabber:iq:roster in the item node.
Yes i know the item Tag is in this namespace, and because of this the
serializer adds it. But i have to Import this node in another
XMLDocument without the attrib (namespace) jabber:iq:roster.

Alex

Nov 11 '05 #5
Alexander Gnauck wrote:
Your Output also has the namespace jabber:iq:roster in the item node.
Yes i know the item Tag is in this namespace, and because of this the
serializer adds it. But i have to Import this node in another
XMLDocument without the attrib (namespace) jabber:iq:roster.

You have to undrstand that namespace is not something you can remove from the
node, node in namespace and node in no namespace are always different nodes,
so you have to recreate node in no namespace to get rid of it. It can be done
in many ways and which is the best one depends on your application logic and
environment. I believe the most effective way is to create custom XmlWriter,
which can then write out XML using another internal XmlTextWriter omitting
namespaces. Nice exercise in custom XmlWriter creation :)
Then you can use this custom writer to serialize the node you need. I can show
you an example if you like this approach.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #6

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

Similar topics

4
by: Ender | last post by:
I am having an issue trying to use the SelectNodes method in C# when using an Xpath Query. I realize lots of people have posted on this subject, but I have a little bit of a different variation. ...
1
by: MacDk | last post by:
Hi, Given this: http://rss.asdf.dk/theothermusic.rss how do you use selectnodes/Xpath to return all title-items (//title is not working). I think it a problem with namespace but i can't...
1
by: Scot NS Curry | last post by:
I have the following XML document. <?xml version="1.0" encoding="utf-16"?> <xd:xmldiff version="1.0" srcDocHash="6067335156532207495" options="None" fragments="no"...
1
by: Jack Colletta | last post by:
How can I parse the component elements per partList in the following xml file? If I remove the namespace section of the xml my code works. I am not sure why the namespace section impacts my code?...
5
by: pneumoconi | last post by:
I've read a lot of posts on this subject each with a slightly different issue and from what I gather my code is fine. But its not. I'm trying to pull out the SQL query from a SQL report document...
2
by: MAF | last post by:
I am triing to query the following xml document. I am loading the xml into a XML Document and then using XPath to get the EntityDataSet Node Here is my code XMLDoc = new...
2
by: Andy | last post by:
Hi all, I have several Xml files, which at the root node change the default namespace, as below: <?xml version="1.0" encoding="UTF-8" ?> <rootNode xmlns="http://www.some.com/namespace/A" /> ...
0
by: Jeppe BS | last post by:
I got this simple XML file which im using the SelectNodes funktion on and it works well. Then i got this other XML file with the same structure as the first one but every sub node contains a lot...
3
by: Sharon | last post by:
I'm trying to navigate inside an XML file that has a very simple namespace, But when I'm using the name space a get nothing (Count == 0) when I do SelectNodes() or SelectSingleNode() (null). When...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.