-1

I am getting the following exception when I am trying to deserialize the xml document. Xml document has a tag as url in which google search link may present. Google search link contains '=' which is not accepted in the xml document while deserializing it. I am getting the xml from server. So I cannot do anything with the string that is present in the url tag. I have to do something on my client part. How can I overcome this problem?

<?xml version="1.0" encoding="UTF-8"?>
<response>
 <status>
  <code>000</code>
  <message>Successfully completed</message>
 </status>
 <reports>
   <report>
      <id>9973</id>
      <url>http://www.google.com/search?q=guns&client=safari&safe=active</url>
   </report>
 </reports>
</response>

Exception :

An exception of type 'System.InvalidOperationException' occurred in System.Xml.XmlSerializer.dll but was not handled in user code

Innerexception:

{"'=' is an unexpected token. The expected token is ';'. Line 136, position 53."}

Fresher
  • 493
  • 6
  • 18
  • How are we going to reproduce this issue? – Patrick Hofman Feb 03 '16 at 11:34
  • "I am getting the xml from server." What server? It sounds like it's simply invalid XML, and whoever is supplying it should fix it. – Jon Skeet Feb 03 '16 at 11:37
  • you probably need HttpUtility.UrlEncode Method – Nahum Feb 03 '16 at 11:38
  • @JonSkeet I have updated my question with the xml that I am getting. – Fresher Feb 03 '16 at 11:52
  • @PatrickHofman i have updated the question for your reference – Fresher Feb 03 '16 at 11:52
  • And yes, that's invalid XML. The `&` should be escaped to `&`. Whatever is generating the XML is broken. That's all we can say at the moment. – Jon Skeet Feb 03 '16 at 11:52
  • 1
    Indeed. Check an online XML validator and you will see it is invalid. – Patrick Hofman Feb 03 '16 at 11:54
  • @NahumLitvin I could only pass the xml to the deserialization function. It is not accepting the xml and throws exception – Fresher Feb 03 '16 at 11:56
  • @JonSkeet It was escaped. I am doing the following string manipulation before passing it to deserialization function. mResponse.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'"); problem is with "=". – Fresher Feb 03 '16 at 12:00
  • 2
    @Fresher: Well don't do that! (And how were we meant to know that you were doing that?) You've converted valid XML into non-XML. That's clearly not going to be a good idea when what you're passing the result to expects XML. – Jon Skeet Feb 03 '16 at 12:01
  • 1
    (And no, the problem isn't the `=` - the problem is that the XML deserializer is expecting `&` to be followed by an entity name, which `=` can't be part of. So it's `&` that's really causing the problem... it's only that it gets *noticed* at `=`.) – Jon Skeet Feb 03 '16 at 12:02
  • @JonSkeet Thanks. Now I know what the actual problem is. But I need to convert & to & so that I could convert &lt; to < and then to <. I will have to do some extra manipulations with the xml string. I will do it. Thank you so much for clarifying the problem. – Fresher Feb 03 '16 at 12:12
  • @Fresher: No, you don't. You shouldn't need to "unescape" the XML at all. Performing string operations on XML like this is a *really bad idea* in almost all cases, and you haven't given any indication of why you'd want to do so at all. – Jon Skeet Feb 03 '16 at 12:17
  • @JonSkeet Actually I am getting the required xml as a string in another xml. so I need to unescape the xml after parsing it out from string tag. While unescaping I also changed & to & which I should not have done. now I removed it as per your advice. everything works fine. thanks. – Fresher Feb 03 '16 at 12:37
  • None of this is clear at all - and *all* this information should have been part of the original question. The chances of manual string replacement being a good idea are miniscule though. – Jon Skeet Feb 03 '16 at 12:54

1 Answers1

2

Your XML is invalid. The URL is breaking the XML standard. Specifically you should escape the &: &amp;.

This is the valid XML:

<?xml version="1.0" encoding="UTF-8"?>
<response>
 <status>
  <code>000</code>
  <message>Successfully completed</message>
 </status>
 <reports>
   <report>
      <id>9973</id>
      <url>http://www.google.com/search?q=guns&amp;client=safari&amp;safe=active</url>
   </report>
 </reports>
</response>

Check your XML export function to make sure it escapes the URL properly.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325