Pages

Tuesday, August 23, 2011

Remove xmlns from XML doc (C#) for use of X-Path

That's right!!!

I was using X-Path to read the nodes from an XML doc, but when I try doing it, it was always wrong.

Why? Because Microsoft Core XML, even the most recent one (you can see it here), only implements the X-Path 1.0 (since 1999).












And this X-Path version does not support the default namespace.

To overpass this problem, you can do 3 things:

  • Add the namespace to the XmlDocument and then use X-Path
  • Remove the namespace from the XmlDocument and then use X-Path
  • Use third dll's that implement newer versions of X-Path

There's some of solutions all over internet, but I prefer an generic solution, like removing all namespaces.

You can do it directly on XmlDocument or use regular expressions.

Personally, I like the regular expressions solution. Here it is:



This way you have an generic solution for all xml document and you can use X-Path with no worries.


Special Thanks to Pedro Lamas giving references and orientation.


References:
http://www.java2s.com/Code/CSharp/XML/Removeallthexmlnamespacesxmlnsattributesinthexmlstring.htm
http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c
http://www.eggheadcafe.com/community/aspnet/2/10021509/problem-with-xpath.aspx
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=3988

4 comments:

  1. Brilliant, thanks, but I can't copy/paste an image....

    ReplyDelete
    Replies
    1. u're right! Sorry for that...

      here you have:

      public static string RemoveAllXmlNamespace(string xmlData)
      {
      string xmlnsPattern = "\\s+xmlns\\s*(:\\w)?\\s*=\\s*\\\"(?[^\\\"]*)\\\"";
      MatchCollection matchCol = Regex.Matches(xmlData, xmlnsPattern);

      foreach (Match m in matchCol)
      {
      xmlData = xmlData.Replace(m.ToString(), "");
      }
      return xmlData;
      }

      Delete
    2. Its an invalid expression

      Delete
    3. No is not...

      is exactly the same has here http://www.java2s.com/Code/CSharp/XML/Removeallthexmlnamespacesxmlnsattributesinthexmlstring.htm (one of the references of the post) and when I use it, worked well...

      Cheers

      Delete