Removing default xmlns from xml

E

EAI

Hi All,

I have a XML of the following form

<?xml version="1.0"?>
<xxxx xmlns="http://xxx.xxx.com">
....
</xxxx>

When I try to read xml using SelectSingleNode, I am getting exception
"Object reference not set to an instance of an object.". But if I remove
xmlns and change xml into

<?xml version="1.0"?>
<xxxx>
....
</xxxx>

then I am able to read the nodes. Can anyone help me to read nodes with out
altering anything? If thats not possible, how do I remove
xmlns="http://xxx.xxx.com" programmatically.

Thanks!
 
G

Guest

Hi EAI,
you do not want to try to remove the namespace declaration from your xxxx
node, instead try to understand what it is doing and use it to your advantage.

The namspace is declared in the element to indicate that the element and
all subelements belong to a particular namespace, this enables you to have
two elements with the same name i.e <Book> but belonging to different
namespace, and therefore your code should process them differently, if they
didn't belong to different namespaces how would you know which element you
had?

Xml namespaces are exactly the same principle as namespaces in .Net. If
you have two types called MyObject in different projects how can you tell
them apart in your code, you add them to different namespaces i.e.

namespace ns1
{
public class MyObject.......
}

namespace ns2
{
public class MyObject......
}

In your example you need to tell XPath what is the namespace associated with
the node, if you do not do this it will not be able to find it in your query.
For example, if we have XML like:


<MyBooks>
<Book xmlns="http://www.foo.com">The Book</Book>
</MyBooks>

In order to select the Book element which belongs to the http://www.foo.com
default namespace you have to do the following in your code:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\test123.xml"); -> this contains the above xml

//create a namespace manager for the document
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);

//add your namespace and give it some arbitrary name i.e. b
//this is the name you will use in your x path to equate to http://www.foo.com
nsm.AddNamespace("b", "http://www.foo.com");

//now in your xpath you need to use the 'b' to reference the namespaced node
XmlNode nd = xmlDoc.SelectSingleNode("/MyBooks/b:Book", nsm);


Hope that helps point you in the right direction.
Mark R Dawson
 
E

EAI

Mark,

Thanks for your reply. I understand the significance of namespaces. But my
case is different.

Thanks!
 
G

Guest

Hi EAI,
I suspect if you are getting a "Object not set to instance of an object"
exception using SelectSingleNode you might be trying to access the properties
of a node that is null because the XPath search is wrong.

For example:

XmlNode nd = xmlDoc.SelectSingleNode("some wrong x path");

//nd is null at this point.
string strName = nd.Attributes["name"].Value

I may be wrong, a copy of your code would hlep to see what is going wrong.


I can use select single node without any problem, by adding the default
namespace to the XmlDocuments namespace table, like in the example below, is
your code like this?

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<Root
xmlns=\"http://www.foo.com\"><somenode></somenode></Root>");

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("b", "http://www.foo.com");
XmlNode nd = xmlDoc.SelectSingleNode("/b:Root/b:somenode", nsm);

Hope that helps
Mark R Dawson
 
E

EAI

Mark,

Thanks for reply. Here is a small part of what I have

<?xml version="1.0"?>
<X12_4010_837
xmlns="http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1">
<X12_EnvelopeHeader>
......
</X12_EnvelopeHeader>

<!-- Other Nodes -->

</X12_4010_837>

The above throws an exception...

<?xml version="1.0"?>
<X12_4010_837>
<X12_EnvelopeHeader>
......
</X12_EnvelopeHeader>

<!-- Other Nodes -->

</X12_4010_837>

The above doesn't. So i want to remove
xmlns="http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1" from
the XMLDocument.

Thanks!

Mark R. Dawson said:
Hi EAI,
I suspect if you are getting a "Object not set to instance of an object"
exception using SelectSingleNode you might be trying to access the
properties
of a node that is null because the XPath search is wrong.

For example:

XmlNode nd = xmlDoc.SelectSingleNode("some wrong x path");

//nd is null at this point.
string strName = nd.Attributes["name"].Value

I may be wrong, a copy of your code would hlep to see what is going wrong.


I can use select single node without any problem, by adding the default
namespace to the XmlDocuments namespace table, like in the example below,
is
your code like this?

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<Root
xmlns=\"http://www.foo.com\"><somenode></somenode></Root>");

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("b", "http://www.foo.com");
XmlNode nd = xmlDoc.SelectSingleNode("/b:Root/b:somenode", nsm);

Hope that helps
Mark R Dawson

EAI said:
Mark,

Thanks for your reply. I understand the significance of namespaces. But
my
case is different.

Thanks!
 
O

Oliver Sturm

EAI wrote:

The above doesn't. So i want to remove
xmlns="http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1" from
the XMLDocument.

Following this discussion, I get the impression there's some
misunderstanding here. Mark is suggesting, I believe, that you try a
different approach to the issue, because there's really no reason at all
to remove the xmlns attribute from your document. I've done this a lot
myself and I think that Mark's suggestion should work fine for you. Have
you tried it with the sample code he gave?

A few messages back you stated that your situation is different. I'm
willing to believe that, but I'd like to know how it's different, so I
may be able to make a different suggestion.

PMFJI, anyway.


Oliver Sturm
 
E

EAI

Oliver,

Thanks for reply. Here is what i tried. I still get the same error message

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlD.NameTable);
nsm.AddNamespace("b",
"http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1");

Used 'nsm' in Select single Node..

It did not work. In this case, I have a XMLDocument. I just want to read
nodes from that xml document. If I delete
'xmlns="http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1"' from
XML, it works fine. For this problem I have two possible solutions. One
deleting the above from XmlDocument. The next being able to read with out
modifying XmlDocument. I followed the way Mark suggested. Since it did not
work I am using a work around solution of using XSLT to eliminate "xmlns".

Thanks!
 
G

Guest

Hi EAI,
I am running out of ideas :) I ran the code below and it seems to work
for me, something else must be different on your end.


XmlDocument xmlDoc;

xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<X12_4010_83
xmlns=\"http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1\"><X12_EnvelopeHeader></X12_EnvelopeHeader></X12_4010_837>");

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("b",
"http://schemas.wpc-edi.com/X12/ISOMORPH/HIPAA/4010A1/TS837Q1");

XmlNode nd1 = xmlDoc.SelectSingleNode("b:X12_4010_837", nsm);
XmlNode nd2 =
xmlDoc.SelectSingleNode("b:X12_4010_837/b:X12_EnvelopeHeader", nsm);


Mark R Dawson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top