2

i have the following xml:

<?xml version="1.0" encoding="WINDOWS-1255"?>
<body>
    <HotelBooking>
        <customers>
            <cust>
                <custID>1111111</custID>
                <title>MR</title>
                <lastName>MASAREWH</lastName>
                <firstName>AHMAD IRAKI</firstName>
            </cust>
            <cust>
                <custID>22222222</custID>
                <title>MRS</title>
                <lastName>HAJ YAHYA IRAQI</lastName>
                <firstName>HIMAT</firstName>
            </cust>
        </customers>
        <Details>
            <name>Dublin & South</name>
        </Details>
    </HotelBooking>
</body>

when i try to serialize it to an object i get an exception. System.InvalidOperationException: There is an error in XML document

after trying to edit this XML using notepad++ XML plug-in tool i understood that the problem is the '&' char in the:

<name>Dublin & South</name>

what are my options here if i don't want to change the xml itself (for example replacing the '&' with 'AND' or something like that)? and are there more chars that can fail my serialization process as well ?

thx for any help!

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Dardar
  • 624
  • 3
  • 13
  • 30

3 Answers3

7

The & character must be escaped: &amp;

<name>Dublin &amp; South</name>

You should be aware that <, >, " and ' must also be escaped (where they are not legal).

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • 2
    Note that you *must* escape this and other entity references: see [wikipedia](http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references) for a full list. – Chris Pfohl Jul 23 '13 at 13:29
  • @Christopher Pfohl What do you mean? "*must* escape ... other entity references". – forty-two Jul 23 '13 at 13:39
  • Just that `&` is not the only character that must be escaped. See the article. Also, that it isn't valid XML if the references *aren't* escaped...so deserializing the text will be (correctly) unsuccessful. – Chris Pfohl Jul 23 '13 at 13:58
1

There are several characters that are not allowed inside xml element body. Such as < > & as well as " and ' in attribute values.

Were are you getting the xml from?

If you are forming it yourself, you should use tools that will take care of any characters that need to be escaped or encoded. Here are a few examples of xml building code: https://stackoverflow.com/a/284331/2610717.

If you have no influence on how it gets to you, use something like System.Security.SecurityElement.Escape().

Bottom line is: see if you can get fix the root of the problem and use framework tools, they are there to make your life easier.

Community
  • 1
  • 1
Taras Dzyoba
  • 121
  • 3
0

the correct presentation of the & is &amp;, so you to change you xml

Swift
  • 1,861
  • 14
  • 17