473,322 Members | 1,699 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,322 software developers and data experts.

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

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

May 6 '06 #1
6 24409
<fz****@calamos.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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
May 6 '06 #2
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

May 8 '06 #3

<fz****@calamos.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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.
May 8 '06 #4
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

May 8 '06 #5

<fz****@calamos.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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.
May 8 '06 #6
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

May 8 '06 #7

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

Similar topics

1
by: Hyunchan Kim | last post by:
To indent xml file, I made an instance of Transformer from Templates using following. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
4
by: David S. Alexander | last post by:
I am trying to transform XML to XML using an XSLT in C#, but the root node of my XML is not being matched by the XSLT if it has an xmlns attribute. Am I handling my namespaces incorrectly? My C#...
0
by: Ewan Fairweather | last post by:
Hi, I'm generating the below XML in c# for an asp.net application When i've generated this XML the <LaboratoryDetails xmlns=""> element has a blank xmlns set. How do i either a) remove this...
1
by: Ewan Fairweather | last post by:
Hi, I'm generating the below XML in c# for an asp.net application When i've generated this XML the <LaboratoryDetails xmlns=""> element has a blank xmlns set. How do i either a) remove this...
6
by: fzhang | last post by:
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...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
If I have an XmlDocument with a deep hierarchy of nodes, with a documentElement having an attribute xmlns="http://someurl.com" and all children inheriting that namespace (but not having the xmlns...
1
by: C.W.Holeman II | last post by:
I have an xmlns attribute that produces an XHTML validation error and I do not understand why it is considered an error.The file displays as expected on Firefox and IE7. ...
2
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, I use Xml serialization to serialize an object into an xml file. My root tag is defined as following:
0
by: rajat teotia | last post by:
Hi, i have created a simple web service using Php Nusoap. its working correctly but the only thing missing is to add the default xmlns attribute to the response tag. Here is the copy of Response :...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.