0

when I serialize the object in XML file I get Reserved characters mentioned in XML below under element "Jobdesc"

Deserializing of the same xml file with these characters gives me

Error:-System.InvalidOperationException was unhandled

Q. What is that I have to do to Deserialize the object...what I am doing wrong???????

XML File

<?xml version="1.0"?>
<DataCheck xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <JobList>
    <Job>
      <JobId>500</JobId>
      <JobDate>1/1/2013</JobDate>
      <Jobdesc>Test the app &#x8;&#x1D;&#x1F;&#x1F;&#x1F;&#x1F;</Jobdesc>
    </Job>
 </JobList>
</DataCheck>

Serialize Code in C#

   XmlSerializer ser = new XmlSerializer(typeof(DataCheck));

    using (FileStream objFS = new FileStream(@"C:\\test.xml", FileMode.Create))
    {
        ser.Serialize(objFS, objDataCheck);
    }

Deserialize code in C#

DataCheck dc;

    FileStream fs1 = new FileStream(@"C:\test.xml", FileMode.Open);

       XmlSerializer xs = new XmlSerializer(typeof(DataCheck));

       dc = (DataCheck)xs.Deserialize(fs1);
Grimmy
  • 3,992
  • 22
  • 25

2 Answers2

1

That is not valid XML. Those characters may not be present in XML, either as entity references (like &#x8;) nor as the actual characters.

This XML can be made valid by using CDATA:

<?xml version="1.0"?>
<DataCheck xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <JobList>
        <Job>
            <JobId>500</JobId>
            <JobDate>1/1/2013</JobDate>
            <Jobdesc><![CDATA[Test the app &#x8;&#x1D;&#x1F;&#x1F;&#x1F;&#x1F;]]></Jobdesc>
        </Job>
    </JobList>
</DataCheck>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • This XML file was generated using XML serialize. which in turn added these characters. – paramveer walia Aug 01 '13 at 01:11
  • I have a class where properties are filed with some text. I then serialize that class which result in XML above. If I append the text of tag jobdesc with <![CDATA[ then angular bracket will also be serilized. – paramveer walia Aug 01 '13 at 01:16
  • 2
    @paramveerwalia - the fact that serialization of string containing characters not valid for XML does not mean XML with this characters become valid... Serialization should have failed, but I think this behavior of creating invalid XML is expected since very early versions of Framework and can't be changed... Your best option is to not have characters that are not valid for XML in your string variables... – Alexei Levenkov Aug 01 '13 at 01:35
  • See http://stackoverflow.com/a/1379936/76337 if you're the one doing the serializing. – John Saunders Aug 01 '13 at 01:37
  • It's not valid within CDATA either. – GoalBased Sep 20 '13 at 18:06
  • @GoalBased: XMLspy considers it to be well-formed XML. – John Saunders Sep 20 '13 at 18:29
  • I think I misread the specification. I think you can have the entity reference, just not the actual character in the CDATA, if that makes sense. I can't change my vote anymore, but if you edit the answer in some small way then it will let me vote this up instead of down. – GoalBased Sep 20 '13 at 21:13
1

Those characters are valid in XML 1.1, so maybe try changing your doctype and hope your parser fully implements 1.1

Hamish Downer
  • 16,603
  • 16
  • 90
  • 84