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

Discussion in 'XML' started by fzhang@calamos.com, May 8, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Peter Flynn Guest

    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/
    Peter Flynn, May 8, 2006
    #2
    1. Advertising

  3. Guest

    Thanks.

    I am looking for solution in C#.

    Frank
    , May 8, 2006
    #3
  4. In article <>,
    <> 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
    Richard Tobin, May 8, 2006
    #4
  5. 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.


    <> wrote in message
    news:...
    >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
    >
    Stan Kitsis [MSFT], May 8, 2006
    #5
  6. John Fullmer Guest

    To remove an xml namespace:

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

    - John Fullmer
    John Fullmer, May 9, 2006
    #6
  7. Greg Collins [Microsoft MVP], May 10, 2006
    #7
  8. Braintrove

    Joined:
    Apr 6, 2007
    Messages:
    5
    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: Apr 6, 2007
    Braintrove, Apr 6, 2007
    #8
  9. lucky1979

    Joined:
    Apr 20, 2010
    Messages:
    1
    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();

    }
    lucky1979, Apr 20, 2010
    #9
  10. ChazZeromus

    Joined:
    Jul 23, 2010
    Messages:
    2
    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.
    ChazZeromus, Feb 25, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RXdhbiBGYWlyd2VhdGhlcg==?=

    Need to remove blank XMLNS attribute from xmldocument

    =?Utf-8?B?RXdhbiBGYWlyd2VhdGhlcg==?=, Apr 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,078
    =?Utf-8?B?RXdhbiBGYWlyd2VhdGhlcg==?=
    Apr 14, 2006
  2. Replies:
    0
    Views:
    1,326
  3. Greg Collins [Microsoft MVP]

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

    Greg Collins [Microsoft MVP], Oct 25, 2006, in forum: XML
    Replies:
    0
    Views:
    825
    Greg Collins [Microsoft MVP]
    Oct 25, 2006
  4. Greg Collins [Microsoft MVP]

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

    Greg Collins [Microsoft MVP], Apr 7, 2007, in forum: XML
    Replies:
    0
    Views:
    677
    Greg Collins [Microsoft MVP]
    Apr 7, 2007
  5. afshar
    Replies:
    3
    Views:
    24,161
    aljar
    May 19, 2010
Loading...

Share This Page