title: Using C# to remove custom xml tags from html
body: <p>I have a string with some html code. However I need to parse that html to a <code>XDocument</code>.</p>
  
  <pre><code>string input = String.Concat("&lt;root&gt;", htmlString, "&lt;/root&gt;");
  var doc = XDocument.Parse(input);
  </code></pre>
  
  <p>But sometimes in my <code>htmlString</code> there is tags like <code>&lt;o:p&gt;&lt;/o:p&gt;</code>, for example, and with that in <code>XDocument.Parse()</code> I got the exception: </p>
  
  <blockquote>
    <p>The ':' character, hexadecimal value 0x3A, cannot be included in a
    name. Line 1, position 650.</p>
  </blockquote>
  
  <p>How can I remove that tags or at least replace the <code>':'</code> in the tag name?</p>
  
  <p>Before doing the parse I'm trying to remove/replace the <code>':'</code> but it isn't working:</p>
  
  <pre><code>try
  {
      Regex regex = new Regex(@"&lt;[:][^&gt;]+&gt;.+?&lt;/\[:]&gt;");
      while (regex.IsMatch(htmlString))
      {
          htmlString= regex.Replace(htmlString, "");
      }
  }
  catch { }
  </code></pre>
  
  <p><strong>HTML example</strong></p>
  
  <pre><code>&lt;p&gt;Some text&lt;/p&gt;
  
  &lt;p class="MsoNormal" style="TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 150%"&gt;
      &lt;?xml:namespace prefix="o" ns="urn:schemas-microsoft-com:office:office"?&gt;
      &lt;o:p&gt; &lt;/o:p&gt;
  &lt;/p&gt;
  
  &lt;p&gt;More text&lt;/p&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm using <code>HtmlAgilityPack</code> but it doesn't remove this tags.</p>
  
  <p><strong><em>My code</em></strong></p>
  
  <pre><code>ConfigureHtmlDocument();
  
  var htmlDoc = new HtmlDocument();
  htmlDoc.OptionFixNestedTags = true;
  htmlDoc.LoadHtml(htmlString);
  
  var htmlError = htmlDoc.ParseErrors.SafeAny();
  
  if (!htmlError)
      htmlString= htmlDoc.DocumentNode.InnerHtml;
  
  try
  {
      Regex regex = new Regex(@"&lt;[:][^&gt;]+&gt;.+?&lt;/\[:]&gt;");
      while (regex.IsMatch(htmlString))
      {
          htmlString= regex.Replace(htmlString, "");
      }
  }
  catch { }
  
  string input = String.Concat("&lt;root&gt;", htmlString, "&lt;/root&gt;");
  var doc = XDocument.Parse(input);
  
  //more code
  </code></pre>
  
  <p><strong><em>ConfigureHtmlDocument()</em></strong></p>
  
  <pre><code>    if (!HtmlNode.ElementsFlags.ContainsKey("p"))
          HtmlNode.ElementsFlags.Add("p", HtmlElementFlag.Closed);
      else
          HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed;
  
      if (!HtmlNode.ElementsFlags.ContainsKey("ul"))
          HtmlNode.ElementsFlags.Add("ul", HtmlElementFlag.Closed);
      else
          HtmlNode.ElementsFlags["ul"] = HtmlElementFlag.Closed;
  
      if (!HtmlNode.ElementsFlags.ContainsKey("li"))
          HtmlNode.ElementsFlags.Add("li", HtmlElementFlag.Closed);
      else
          HtmlNode.ElementsFlags["li"] = HtmlElementFlag.Closed;
  
      if (!HtmlNode.ElementsFlags.ContainsKey("ol"))
          HtmlNode.ElementsFlags.Add("ol", HtmlElementFlag.Closed);
      else
          HtmlNode.ElementsFlags["ol"] = HtmlElementFlag.Closed;
  
      //more similar code
  </code></pre>