How to remove all namespaces from broken XML with C#?

Here is how to remove all namespace from xml. But it is not working for me. Because sometimes I am getting broken xml feed. eg:

<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress.com" -->
<rss version="2.0"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<channel>
  <title>sabri ?lker - WordPress.com Search</title>
  <link>http://tr.search.wordpress.com/?q=sabri+%C3%BClker&page=2&t=comment&s=date</link>
  <description>sabri ?lker - WordPress.com Search</description>
  <pubDate>Fri, 04 Jan 2013 08:58:41 +0000</pubDate>
  <language>tr</language>
  <image><url>http://s.wordpress.com/i/buttonw-com.png</url><width>224</width><height>58</height><title>WordPress.com</title><link>http://wordpress.com/</link></image>
  <generator>http://search.wordpress.com/</generator>
  <atom:link rel="self" type="application/rss+xml" href="http://tr.search.wordpress.com/?q=sabri+%C3%BClker&page=2&t=comment&s=date&amp;f=feed" />
  <atom:link rel="search" type="application/opensearchdescription+xml" href="http://en.search.wordpress.com/opensearch.xml" title="WordPress.com" />
  <opensearch:totalResults>10</opensearch:totalResults><opensearch:startIndex>11</opensearch:startIndex><opensearch:itemsPerPage>10</opensearch:itemsPerPage><opensearch:Query role="request" searchTerms="sabri ?lker startPage="2" /></channel>
</rss>

my exceptiom is “Name cannot begin with the ‘2’ character, hexadecimal value 0x32. Line 17, position 227.” to the result. So what should I do to solved this problem.

Related posts

Leave a Reply

1 comment

  1. I’d say the reason is the ill-formed searchTerms attribute:

    searchTerms="sabri ?lker startPage="2"  
    

    It’s quoted the wrong way it should use &quot; instead of “. You could simply replace all ” with &quot;

    string input = ..; // your xml
    string processedInput = input.Replace(""", "&quot;");
    
    // then feed this into your xml parser.
    

    This should solve your issue, but it’s of course not a general way of sanitizing wrong xml input. You may want to have a look at http://tidyfornet.sourceforge.net/ it can sanitize HTML, XHTML and XML.