473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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>"
Nov 17 '05 #1
2 3024
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
"Ste" <no*****@nospam.com> wrote in message
news:hY********************@pipex.net...
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>"
Nov 17 '05 #2
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 !


"Ollie Riches" <ol**********@phoneanalyser.net> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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
"Ste" <no*****@nospam.com> wrote in message
news:hY********************@pipex.net...
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>"

Nov 17 '05 #3

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

Similar topics

9
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
0
by: Andrew Burgher | last post by:
Feeding the following .xsd into the XsdObjectGen (v1.4.2.0) tool produces an invalid attribute: with DataType="System.String". Has anybody seen this behaviour before? Is this a bug in...
0
by: TT (Tom Tempelaere) | last post by:
Hi there, I'm using the XSDObjectGen tool to generate a class described as an XSD. <url...
0
by: Tommy Christian | last post by:
Hi! Anyone who knows about saving serialized data to database, coz I have a problem with that. If I just serialize my session data and then deserialize it, it works. But when I save it...
0
by: Jack Fox | last post by:
Some dotnet functionality only works in conjunction with streams. A couple of times I have only wanted to place the results in a string. This is one of those cases. Is there any way I can do this...
0
by: wc chan | last post by:
I generated an object class by using xsdobjectgen from MS. The object class was OK in our programs. However, when we want to pass the object to a webservice, we got problem. We found that the proxy...
0
by: Don Leckie | last post by:
Hi, I hope someone can really help me. This problem has me stumped. I have class "A" that does not implement Serializable, but class "A" subclasses Serializable class "B". Therefore, when...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
2
by: audiokarate | last post by:
I have like atleast 15 errors that say class, interface, or enum expected and I have no idea what that means. Can someone tell me what it is and how I might be able to fix it? Thank you. - Manny...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.