How to remove xmlns attribute from XML document (.net)

F

fzhang

I am relatively new to XML and C#. So, forgive me if this question is
too newbie. :)

While assuming this is an easy programming task, I couldn't find a
single reference anywhere for how to do it. Here is the situation:

I am given an XML file like the one below from other group in my
company to load the data into our database.

<root xmlns="the-namespace">
....
data here...
....
</root>

I was able to load the file into an XmlDocument object and then into db
tables only when I removed 'xmlns="the-namespace'. However, if I
didn't remove it, The dataset in the XmlDocument was empty and there
was no exception thrown. I'd like to find a way of programmatically
removing the xmlns attribute. BTW, the namespace in the file was not
referenced anywhere in the XML document.

Thanks in advance.

Frank
 
R

Richard Blewett [DevelopMentor]

I am relatively new to XML and C#. So, forgive me if this question is
too newbie. :)

While assuming this is an easy programming task, I couldn't find a
single reference anywhere for how to do it. Here is the situation:

I am given an XML file like the one below from other group in my
company to load the data into our database.

<root xmlns="the-namespace">
...
data here...
...
</root>

I was able to load the file into an XmlDocument object and then into db
tables only when I removed 'xmlns="the-namespace'. However, if I
didn't remove it, The dataset in the XmlDocument was empty and there
was no exception thrown. I'd like to find a way of programmatically
removing the xmlns attribute. BTW, the namespace in the file was not
referenced anywhere in the XML document.

Thanks in advance.

Frank

Namespaces in XML are used in exactly the same way as namespaces in C#, to
disambiguate things.

Imagine you had an XML document descibing river navigation you much have an
element called "channel". If you have a document about TV programs you might
have an element called "channel". The problem comes when you have a document
about a TV program about river navigation - how do you tell the two channel
elements apart? XML namespaces.

There are two syntaxes for XML namespaces: one explicitly sets elements into
the namespace

<x:program xmlns:x="uri-TV">
<x:channel />
</x:program>

the other does it implicitly

<program xmlns="uri-TV>
<channel />
</program>

in the second example the program and channel elements are in the namespace
because the namespace is said to be the default for program element and all
its children.

Now to your question: what makes you think that the XML document you hahve
received hasn't been loaded into your XmlDocument instance?

Also you are using the terms DataSet and XmlDocument seemingly
interchangeably - where does the DataSet some from?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
F

fzhang

Thanks for your reply. I understand what 'namespace' is. However, in
our particular case, the data files (in XML) are generated by someone
else. That particular namespace may mean something to them but nothing
to us. As I mentioned in my previous post, if I manually removed
'xmlns="the-namespace"' from the XML data file, my program could parse
and load the data to the XmlDataDocument object without any problem.
However, if I didn't, the dataset in the XmlDataDocument object was
empty.

Here is my code (We have a schema and a data files ready for data
loading)
........
m_xDataDoc = new XmlDataDocument();
Debug.Assert(m_xDataDoc != null);
m_xDataDoc.DataSet.ReadXmlSchema(m_SchemaFile);

Debug.Assert(m_DataFile != null);
m_xDataDoc.Load(m_DataFile);
........

If I don't remove the namespace, m_xDataDoc.DataSet is empty. If I
manually remove the namespace, m_xDataDoc.DataSet properly contains
data from the XML file.

I just would like to know if we can programmatically remove the
namespace to make the data (XML) file loadable.

Frank
 
N

Nick Hounsome

Thanks for your reply. I understand what 'namespace' is. However, in
our particular case, the data files (in XML) are generated by someone
else. That particular namespace may mean something to them but nothing
to us. As I mentioned in my previous post, if I manually removed
'xmlns="the-namespace"' from the XML data file, my program could parse
and load the data to the XmlDataDocument object without any problem.
However, if I didn't, the dataset in the XmlDataDocument object was
empty.

Here is my code (We have a schema and a data files ready for data
loading)
........
m_xDataDoc = new XmlDataDocument();
Debug.Assert(m_xDataDoc != null);
m_xDataDoc.DataSet.ReadXmlSchema(m_SchemaFile);

Debug.Assert(m_DataFile != null);
m_xDataDoc.Load(m_DataFile);
........

If I don't remove the namespace, m_xDataDoc.DataSet is empty. If I
manually remove the namespace, m_xDataDoc.DataSet properly contains
data from the XML file.

I just would like to know if we can programmatically remove the
namespace to make the data (XML) file loadable.

Frank

What's your schema file?
The namespace in the schema should match the document.
You can probably make it work by setting the namespace in the schema.
 
F

fzhang

What's weird is that the particular namespace is not in the schema
file. It's a separate issue why this namespace even gets in the data
file but I won't go there. :-(

Anyway, since this namespace is useless to us and I can get the file
loaded properly after I manually removed it, I am not interested in
this particular namespace at all. All I need is a programmatic way of
removing it.

Thanks.

Frank
 
N

Nick Hounsome

What's weird is that the particular namespace is not in the schema
file. It's a separate issue why this namespace even gets in the data
file but I won't go there. :-(

But that's your problem:

Your schema describes elements in an unnamed namespace whereas your data is
declared to be in a named namespace hence the schema cannot possibly match
the document.

Add a namespace to the schema to match elements and your problem will go
away. Your proposed solution of hacking the document is precisely that - a
hack.

Anyway, since this namespace is useless to us and I can get the file
loaded properly after I manually removed it, I am not interested in
this particular namespace at all. All I need is a programmatic way of
removing it.

It's not useless because it defines the namespace.
What you are saying is the equivalent of a C# attempt to instantiate a class
called ArrayList - There is no such thing. There is only
System.Collections.ArrayList. You can get away with calling it ArrayList but
only because you say "using System.Collections;" which is the C# equivalent
of the xmlns in your document.

[Actually you are doing the opposite - Your schema is the equivalent of
declaring a class called ArrayList outside any namespace - You then try to
instantiate a System.Collections.ArrayList]

namespaces in XML are not just decoration - they are fundamental.
 
F

fzhang

Ok, could you show me how to programmatically add the namespace to the
schema? While I understand the namespace concept and theory, I have to
find a solution of the existing situation -- a schema file that does
not contain the particular namespace.

I tried the following before but it did not work.

XmlNamespaceManager nsm = new
XmlNamespaceManager(m_xDataDoc.NameTable);
nsm.AddNamespace("xmlns", "the-namespace");


Frank
 

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