Serialized class from XSDObjectGen results in invalid char at position 1,1

S

Ste

I generated a class from an XSD using XSDObjectGen, when i try to create an xml serialized string from it i get an invalid char in position 1,1

The code i use to serialise the object is pasted below.. lifted from http://www.dotnetjohn.com/articles.aspx?articleid=173

The char at position 1 is displayed as a "?" so some encoding is taking place.. it has a hex value is 0xFeFF

Can anyone spot an obvious mistake??




private String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters);
return (constructedString);
}


XmlInput inp = new XmlInput();
inp.AddSomething();

MemoryStream memoryStream = new MemoryStream();
memoryStream.Position = 0;
XmlSerializer xs = new XmlSerializer(typeof(XmlInput));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, inp);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
System.Console.WriteLine(XmlizedString);


... output xml all looks ok ...apart from leading char


"?<?xml version=\"1.0\" encoding=\"utf-8\"?><XmlInput xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:My.XmlInput\"><Service>XXX</Service</XmlInput>"
 
O

Ollie Riches

try the following and tell us what you get:

XmlInput inp = new XmlInput();
inp.AddSomething();

System.Text.StringBuilder sbSer = new System.Text.StringBuilder();
using(System.IO.StringWriter sw = new System.IO.StringWriter(sbSer))
{
XmlSerializer ser = new XmlSerializer(typeof(XmlInput));
ser.Serialize(sw, inp);
}
Console.WriteLine(sbSer.ToString()));

HTH

Ollie Riches


I generated a class from an XSD using XSDObjectGen, when i try to create an
xml serialized string from it i get an invalid char in position 1,1

The code i use to serialise the object is pasted below.. lifted from
http://www.dotnetjohn.com/articles.aspx?articleid=173

The char at position 1 is displayed as a "?" so some encoding is taking
place.. it has a hex value is 0xFeFF

Can anyone spot an obvious mistake??




private String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters);
return (constructedString);
}


XmlInput inp = new XmlInput();
inp.AddSomething();

MemoryStream memoryStream = new MemoryStream();
memoryStream.Position = 0;
XmlSerializer xs = new XmlSerializer(typeof(XmlInput));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8);
xs.Serialize(xmlTextWriter, inp);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
System.Console.WriteLine(XmlizedString);


... output xml all looks ok ...apart from leading char


"??<?xml version=\"1.0\" encoding=\"utf-8\"?><XmlInput
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"urn:My.XmlInput\"><Service>XXX</Service</XmlInput>"
 
S

Ste

That worked a treat. thanks....

I actually fixed the original version

by replacing

XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8);

with

XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new
UTF8Encoding());

Which is the most efficient.. if minimum lines or code where the issue -
your version would win hands down !
 

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