title: '<', hexadecimal value 0x3C, is an invalid attribute character error after replacing illegal chars
body: <p>I am parsing xml and when the xml is in a bad format it skips using the xml document and goes through regular text parsing. I replace all illegal xml characters but I am still getting this error appear randomly;</p>
  
  <p>'&lt;', hexadecimal value 0x3C, is an invalid attribute character</p>
  
  <p>Why do xml errors still crop up if you take the xml text put in a text file and parse it regularly using string parsing or regex stuff. I can see the content after replacing illegal characters and it still won't load into a xml doc and still gives errors in the regular parsing.</p>
  
  <p>cleanup methods:</p>
  
  <pre><code>Private Shared Function replaceIllegalXMLChars(ByVal item As String) As String
  
      Dim returnValue As String = item
      returnValue = returnValue.Replace("&amp;", "&amp;amp;")
      returnValue = returnValue.Replace("""", "&amp;quot;")
      returnValue = returnValue.Replace("'", "&amp;apos;")
      returnValue = returnValue.Replace("&lt;", "&amp;lt;")
      returnValue = returnValue.Replace("&gt;", "&amp;gt;")
  
      Return returnValue
  
  End Function
  
  Private Shared Function replaceIllegalChars(ByVal state As String) As String
      'remove any hexdecimal characters like &amp;#x19; and &amp;#x0;
      state = Regex.Replace(state, "&amp;\#x([0-9A-F]{1}[0-9A-F]{0,1});", " ")
      'remove any hexdecimal characters like &amp;#19; and &amp;#0;  (no x's)
      state = Regex.Replace(state, "&amp;\#([0-9A-F]{1}[0-9A-F]{0,1});", " ")
      state = Regex.Replace(state, "0xFFFF", " ")
      'added on 3/19/2008 to fix a non-encoded character issue, mostly for LJ
      state = Regex.Replace(state, "&amp;nbsp;", "&amp;amp;nbsp;")
      'mark as completed
      Return state
  End Function
  </code></pre>
  
  <p>calling function content</p>
  
  <pre><code>         Dim xdoc As XmlDocument = New XmlDocument
          _html = replaceIllegalChars(_html)
          _html = replaceIllegalXMLChars(_html)
          _html = checkUnicode(_html)
  
          ' now load the file as a xml file
          xdoc.LoadXml(_html)
  </code></pre>
  
  <p>some times I get the error Data at the root level is invalid. Line 1, position 1,
  is there any clear cut methods of cleaning up xml and parsing it.</p>
  
  <p>if I were not to format the xml but just take the raw text and parse it, then why do xml errors still crop up, should it not just be considered to be text and if not then is there anyway other than manually to go through the doc and find all errors in tags etc and correct it.</p>