title: xml escape special characters
body: <p>Create a file with this content:</p>
  
  <pre><code>&lt;xml&gt;yen symbol - ¥&lt;/xml&gt;
  </code></pre>
  
  <p>Open the file in firefox, you get this error :</p>
  
  <pre><code>XML Parsing Error: not well-formed
  Location: file:///test.xml
  Line Number 1, Column 19:&lt;xml&gt;yen symbol - &lt;/xml&gt;
  ------------------^
  </code></pre>
  
  <p>How can I escape the special characters in XML ?</p>
  
  <p>NOTE : I'm using .Net XmlDocument.OuterXML property to retrieve the XML. For some reason, .net doesnt escape the yen character automatically. </p>
  
  <p>Update: The real problem I have is I construct the xml in .net through code and push the xml over http to Solr. Java code inside solr breaks because it considers the yen character as malformed xml. I set the encoding to UTF-8.</p>
  
  <pre><code>Public Shared Sub UpdateRecords(p_SolrRecordCollection As SolrRecordCollection, Optional commit As Boolean = True, Optional optimize As Boolean = True)
              Try
                  Dim webClientInstance As New WebClient()
                  webClientInstance.Headers.Add("Content-Type", "text/xml")
                  webClientInstance.Encoding = System.Text.Encoding.UTF8
                  Dim xml = p_SolrRecordCollection.XmlDocument.OuterXml
                  Dim params As String = String.Format("?commit={0}&amp;optimize={1}", commit.ToString.ToLower, optimize.ToString.ToLower)
                  webClientInstance.UploadString(SolrURL + UpdateRelativeURL + params, xml)
              Catch ex As WebException
                  Dim responseText As String = String.Empty
                  If ex.Response IsNot Nothing Then
                      responseText = " :" &amp; ControlChars.NewLine
                      Using reader = New StreamReader(ex.Response.GetResponseStream())
                          responseText = reader.ReadToEnd()
                      End Using
                  End If
                  Throw New Exception("Request to Solr failed" &amp; responseText, ex)
              End Try
          End Sub
  </code></pre>
  
  <p>This is the error reported by Solr</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;response&gt;
  &lt;lst name="responseHeader"&gt;&lt;int name="status"&gt;500&lt;/int&gt;&lt;int name="QTime"&gt;135&lt;/int&gt;&lt;/lst&gt;&lt;lst name="error"&gt;&lt;str name="msg"&gt;[com.ctc.wstx.exc.WstxLazyException] Illegal character entity: expansion character (code 0xb) not a valid XML character
   at [row,col {unknown-source}]: [827,871]&lt;/str&gt;&lt;str name="trace"&gt;[com.ctc.wstx.exc.WstxLazyException] com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character (code 0xb) not a valid XML character
   at [row,col {unknown-source}]: [827,871]
      at com.ctc.wstx.exc.WstxLazyException.throwLazily(WstxLazyException.java:45)
      at com.ctc.wstx.sr.StreamScanner.throwLazyError(StreamScanner.java:729)
      at com.ctc.wstx.sr.BasicStreamReader.safeFinishToken(BasicStreamReader.java:3659)
      at com.ctc.wstx.sr.BasicStreamReader.getText(BasicStreamReader.java:809)
      at org.apache.solr.handler.loader.XMLLoader.readDoc(XMLLoader.java:393)
      at org.apache.solr.handler.loader.XMLLoader.processUpdate(XMLLoader.java:245)
      at org.apache.solr.handler.loader.XMLLoader.load(XMLLoader.java:173)
      at org.apache.solr.handler.UpdateRequestHandler$1.load(UpdateRequestHandler.java:92)
      at org.apache.solr.handler.ContentStreamHandlerBase.handleRequestBody(ContentStreamHandlerBase.java:74)
      at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)
      at org.apache.solr.core.SolrCore.execute(SolrCore.java:1817)
      at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:639)
      at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:345)
      at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:141)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
      at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
      at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
      at org.eclipse.jetty.server.Server.handle(Server.java:365)
      at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
      at org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:53)
      at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
      at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
      at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:642)
      at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
      at org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:72)
      at org.eclipse.jetty.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:264)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
      at java.lang.Thread.run(Unknown Source)
  Caused by: com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character (code 0xb) not a valid XML character
   at [row,col {unknown-source}]: [827,871]
      at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:630)
      at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:461)
      at com.ctc.wstx.sr.StreamScanner.reportIllegalChar(StreamScanner.java:2400)
      at com.ctc.wstx.sr.StreamScanner.checkAndExpandChar(StreamScanner.java:2346)
      at com.ctc.wstx.sr.StreamScanner.resolveSimpleEntity(StreamScanner.java:1205)
      at com.ctc.wstx.sr.BasicStreamReader.readTextSecondary(BasicStreamReader.java:4677)
      at com.ctc.wstx.sr.BasicStreamReader.readCoalescedText(BasicStreamReader.java:4126)
      at com.ctc.wstx.sr.BasicStreamReader.finishToken(BasicStreamReader.java:3701)
      at com.ctc.wstx.sr.BasicStreamReader.safeFinishToken(BasicStreamReader.java:3649)
      ... 36 more
  &lt;/str&gt;&lt;int name="code"&gt;500&lt;/int&gt;&lt;/lst&gt;
  &lt;/response&gt;
  </code></pre>