Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to remove xmlns attribute from XML document (.net)

Reply
Thread Tools

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

 
 
fzhang@calamos.com
Guest
Posts: n/a
 
      05-08-2006
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

 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      05-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
fzhang@calamos.com
Guest
Posts: n/a
 
      05-08-2006
Thanks.

I am looking for solution in C#.

Frank

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-08-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      05-08-2006
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.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
John Fullmer
Guest
Posts: n/a
 
      05-09-2006

To remove an xml namespace:

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

- John Fullmer

 
Reply With Quote
 
Greg Collins [Microsoft MVP]
Guest
Posts: n/a
 
      05-10-2006
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 )


 
Reply With Quote
 
Braintrove Braintrove is offline
Junior Member
Join Date: Apr 2007
Posts: 5
 
      04-06-2007
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 by Braintrove; 04-06-2007 at 09:42 PM..
Reply With Quote
 
lucky1979 lucky1979 is offline
Junior Member
Join Date: Apr 2010
Posts: 1
 
      04-20-2010
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.NameTabl e);

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_A LIAS))
{
nsmgr.AddNamespace(Constants.DEFAULT_NAMESPACE_ALI AS, 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[i]))
{
if (!ary[i].Contains(":") && !ary[i].Contains("*") && !ary[i].Contains("."))
{
sb.Append(Constants.DEFAULT_NAMESPACE_ALIAS + ":");
}
sb.Append(ary[i]);
}

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

return sb.ToString();

}
 
Reply With Quote
 
ChazZeromus ChazZeromus is offline
Junior Member
Join Date: Jul 2010
Posts: 2
 
      02-25-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
What does xmlns:xsi and xmlns:xsd attributes mean? afshar XML 3 05-19-2010 02:16 AM
Re: How to remove xmlns attribute from XML document (.net) Greg Collins [Microsoft MVP] XML 0 04-07-2007 03:42 PM
Re: How to remove xmlns attribute from XML document (.net) Greg Collins [Microsoft MVP] XML 0 10-25-2006 04:31 PM
xmlns:SOAP-ENC, xmlns:xsd required? chris.stromberger@gmail.com XML 0 10-26-2005 02:48 PM



Advertisments