XmlSerializer and invalid string chars?

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

Had a method that got some string info from mp3 tags in N files and
serializes this class and deserializes at other side. Works ok except
sometimes get chars that choke the XmlSerializer. After some digging, I
found XmlSerializer chokes on 0x03 chars. It probably chokes on many
others, but this one I found. It serializes ok, but chokes on deserialize
on "<Field1></Field1>". So the questions are:
1) Why does serializer produce output that can't be deserialized? Is this a
bug?
2) What should I do about it?
3) Do I need to preprocess all strings to replace any invalid chars with "?"
or something and what Set of chars should I be worried about?

Code sample below:

private void button11_Click(object sender, System.EventArgs e)
{
Test t = new Test();
t.Field1 = ((char)0x3).ToString();
string xml = t.ToXmlString();
Console.WriteLine(xml);
Test t2 = Test.FromXmlString(xml);
Console.WriteLine("Done");
}

public class Test
{
public string Field1;

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(Test));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static Test FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

Test xLic = null;
try
{
XmlSerializer ser = new XmlSerializer(typeof(Test));
using (StringReader sr = new StringReader(xmlString))
{
xLic = (Test)ser.Deserialize(sr);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
return xLic;
}
}

Output:

<?xml version="1.0" encoding="utf-16"?>
<Test xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Field1></Field1>
</Test>
System.InvalidOperationException: There is an error in XML document (3,
4). ---> System.Xml.XmlException: '', hexadecimal value 0x03, is an invalid
character. Line 3, position 14.
at System.Xml.XmlScanner.ScanHexEntity()
at System.Xml.XmlTextReader.ParseBeginTagExpandCharEntities()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read1_
Test(Boolean isNullable, Boolean checkType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read4_
Test()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at NetLicenseTest.Test.FromXmlString(String xmlSAn unhandled exception of
type 'System.InvalidOperationException' occurred in NetLicenseTest.exe

Additional information: There is an error in XML document (3, 4).

tring) in c:\documents and settings\administrator\my documents\visual studio
projects\netlicensetest\media.cs:line 572
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top