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>&# x3;</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.EventArg s e)
{
Test t = new Test();
t.Field1 = ((char)0x3).ToS tring();
string xml = t.ToXmlString() ;
Console.WriteLi ne(xml);
Test t2 = Test.FromXmlStr ing(xml);
Console.WriteLi ne("Done");
}
public class Test
{
public string Field1;
public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(t ypeof(Test));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
data = sw.ToString();
return data;
}
}
public static Test FromXmlString(s tring xmlString)
{
if ( xmlString == null )
throw new ArgumentNullExc eption("xmlStri ng");
Test xLic = null;
try
{
XmlSerializer ser = new XmlSerializer(t ypeof(Test));
using (StringReader sr = new StringReader(xm lString))
{
xLic = (Test)ser.Deser ialize(sr);
}
}
catch(Exception ex)
{
Console.WriteLi ne(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.InvalidO perationExcepti on: There is an error in XML document (3,
4). ---> System.Xml.XmlE xception: '', hexadecimal value 0x03, is an invalid
character. Line 3, position 14.
at System.Xml.XmlS canner.ScanHexE ntity()
at System.Xml.XmlT extReader.Parse BeginTagExpandC harEntities()
at System.Xml.XmlT extReader.Read( )
at System.Xml.XmlR eader.ReadEleme ntString()
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReader1.Read1 _
Test(Boolean isNullable, Boolean checkType)
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReader1.Read4 _
Test()
--- End of inner exception stack trace ---
at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
xmlReader, String encodingStyle, XmlDeserializat ionEvents events)
at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
xmlReader, XmlDeserializat ionEvents events)
at System.Xml.Seri alization.XmlSe rializer.Deseri alize(TextReade r
textReader)
at NetLicenseTest. Test.FromXmlStr ing(String xmlSAn unhandled exception of
type 'System.Invalid OperationExcept ion' occurred in NetLicenseTest. exe
Additional information: There is an error in XML document (3, 4).
tring) in c:\documents and settings\admini strator\my documents\visua l studio
projects\netlic ensetest\media. cs:line 572
--
William Stacey, MVP
http://mvp.support.microsoft.com