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>

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
 
P

Peter Flynn

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
 
R

Richard Tobin

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
 
S

Stan Kitsis [MSFT]

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.
 
J

John Fullmer

To remove an xml namespace:

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

- John Fullmer
 
Joined
Apr 6, 2007
Messages
5
Reaction score
0
Updated link

Please note that the Braintrove site structure has changed.
The above article can now be accessed at its now location:

Prevent Namespace Prefixes from Being Copied to the Output
 
Last edited:
Joined
Apr 20, 2010
Messages
1
Reaction score
0
Instead of removing the namespace from your code you can easily live with this and query the xml with the help of xmlnamespacemanager.

following is the Solution for the same


1. Get the namespaceManager for all the default namespaces
///Pass the document as XMLElement

public static XmlNamespaceManager GetNamespaceManager(XmlElement Element)
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(Element.OwnerDocument.NameTable);

XmlNode node = (XmlNode)Element;
RecurseNamespaces(node, nsmgr);

return nsmgr;
}


private static void RecurseNamespaces(XmlNode node, XmlNamespaceManager nsmgr)
{
if (node.Attributes != null)
{
foreach (XmlAttribute attribute in node.Attributes)
{
if (attribute.Name.StartsWith(Constants.XML_NAMESPACE_PREFIX, StringComparison.CurrentCultureIgnoreCase))
{
if (attribute.LocalName == attribute.Name)
{
if (!nsmgr.HasNamespace(Constants.DEFAULT_NAMESPACE_ALIAS))
{
nsmgr.AddNamespace(Constants.DEFAULT_NAMESPACE_ALIAS, attribute.Value);
}
}
else
{
if (!nsmgr.HasNamespace(attribute.LocalName))
{
nsmgr.AddNamespace(attribute.LocalName, attribute.Value);
}
}
}
}
}

if (node.HasChildNodes)
{
foreach (XmlNode childnode in node.ChildNodes)
{
RecurseNamespaces(childnode, nsmgr);
}
}

}


///Pass the xpath to following function to get the updated xpath to query
public static string GetDefaultNamespaceXPath(string XPath)
{
string[] ary = XPath.Split('/');
StringBuilder sb = new StringBuilder();

for (int i = 0; i < ary.Length; i++ )
{
if (!string.IsNullOrEmpty(ary))
{
if (!ary.Contains(":") && !ary.Contains("*") && !ary.Contains("."))
{
sb.Append(Constants.DEFAULT_NAMESPACE_ALIAS + ":");
}
sb.Append(ary);
}

if (i < ary.Length - 1)
{
sb.Append("/");
}
}

return sb.ToString();

}
 
Joined
Jul 23, 2010
Messages
2
Reaction score
0
I was searching google and found this thread, and if you use XmlDocument and want to rid of namespaces, create a XmlTextReader object and set the Namespaces property to false then pass it in the XmlDocument.Load method to read the xml document without the use of namespaces. This is particularly useful to ignore namespace rules to create simpler XPath expressions.
Also set the Namespaces property in the XmlTextWriter when trying to output formatted xml this way.
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top