473,591 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer and invalid string chars?

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>&#x3; </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
Nov 16 '05 #1
0 2287

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5412
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <?xml version="1.0" encoding="utf-8"?> <MyRootClass...
3
4503
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as XmlText. In my main class, i have created an arraylist which contains a collection of this class FormField. Basically its: public void Add( string sName, string sValue )
4
1984
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the following error:
3
3164
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on the server (W2K3) it throws an exception. It only seems to happen when serializing/deserializing _arrays_ of a type. If I just serialize/deserialize one instance, it works fine. The exception I get is: (sorry for the word wrapping.)...
3
10398
by: stax | last post by:
Hello, can somebody tell me how to serialize/deserialize a object containing a multi line string using the XmlSerializer class. One of the both windows linefeed chars get dumped somewhere down the road. It would be awesome somebody could answer this, sadly most of my questions don't get answered. Regards,
1
2078
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two inconsistencies that Xerces is able to handle that XmlSerializer seems not to be able to deal with. 1) Some of the boolean values that I'm dealing with are "True" and "False" rather than "true" and "false". I'm not quite sure whether "True" and...
0
1177
by: swatisahasrabudhe | last post by:
I have a class that I serialize/deserialize using xmlserializer. There are a few properties with char datatype. I have a few xml files from which I want to create the objects. While deserializing from xml if the properties having char datatypes have numeric values(0-9) then everything worls fine. If I put any letter e.g. A it starts throwing exception - "Input string is not in correct format". If I convert these properties to string, all...
6
5181
by: preport | last post by:
I have a webservice that returns data from a database. Our services (the clients) have been blowing up because of illegal character problems. Is there anything I can do on the server side to work around this problem. I'm OK with deleting all the "bad" characters. I have a couple DTOs that are marked with that I return from the web service after they're populated with data from the database. Is there some kind of attribute or...
6
3695
by: Andrew | last post by:
I am using an XmlSerializer to save some settings and I have discovered that when a string is saved containing a cr+nl it is replaced with just a newline when loading back in. I am no expert with XML and I wonder if there is a option I have to change to fix this. I have written some simple test code to show the problem. Can anyone please tell me why the cr+nl is being replaced by just a newline and how to stop this happening? Thanks
0
7870
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.