470,841 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

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>

The particular namespace may mean something to the people who generate
the xml data schema and data files but nothing to us.

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 by hand in the data file,
m_xDataDoc.DataSet is empty. If I
manually remove the namespace, m_xDataDoc.DataSet properly contains
data from the XML file.
While I understand 'namespace' and its usage, I would just like to
know if we can programmatically (instead of manually) remove the
namespace to make the data (XML) file loadable.
Thanks in advance.
Frank

May 8 '06 #1
6 3065
fz****@calamos.com wrote:
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>

The particular namespace may mean something to the people who generate
the xml data schema and data files but nothing to us.

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 by hand in the data file,
m_xDataDoc.DataSet is empty. If I
manually remove the namespace, m_xDataDoc.DataSet properly contains
data from the XML file.
While I understand 'namespace' and its usage, I would just like to
know if we can programmatically (instead of manually) remove the
namespace to make the data (XML) file loadable.


Pass the file through a scriptable stream editor like sed, eg

sed -e "s+<root[^>]*>+<root>+"

Sed for Windows is available from GNU or Cygwin.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
May 8 '06 #2
Thanks.

I am looking for solution in C#.

Frank

May 8 '06 #3
In article <11**********************@g10g2000cwb.googlegroups .com>,
<fz****@calamos.com> wrote:
I am looking for solution in C#.


Write a program in C# that removes the string?

If you want to remove a namespace by processing the document as XML
it's going to be tedious, since you're changing all the elements to be
in a different namespace. You can't just remove the attribute unless
there's some way to disable namespace processing, because once you've
parsed the file it has already had its effect on all the other
elements.

-- Richard
May 8 '06 #4
1. Read the file
2. look for xmlns="..."
3. remove it

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
<fz****@calamos.com> wrote in message
news:11**********************@v46g2000cwv.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>

The particular namespace may mean something to the people who generate
the xml data schema and data files but nothing to us.

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 by hand in the data file,
m_xDataDoc.DataSet is empty. If I
manually remove the namespace, m_xDataDoc.DataSet properly contains
data from the XML file.
While I understand 'namespace' and its usage, I would just like to
know if we can programmatically (instead of manually) remove the
namespace to make the data (XML) file loadable.
Thanks in advance.
Frank

May 8 '06 #5

To remove an xml namespace:

XmlNamespaceManager xnm = new XmlNamespaceManager("[xmlnamedtable]");
xnm.RemoveNamespace("[namespace prefix]", "[uri]");

- John Fullmer

May 10 '06 #6
Is this namespace appearing as part of the output of a transform? If so check out this how to article:

Prevent Namespace Prefixes from Being Copied to the Output
http://www.braintrove.com/default.aspx?p=3&s=1&i=8

--
Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
May 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ewan Fairweather | last post: by
3 posts views Thread by =?Utf-8?B?R3JlZw==?= | last post: by
1 post views Thread by C.W.Holeman II | last post: by
2 posts views Thread by =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.