By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Serializing a class and not get the "?xml version"

P: n/a
I am using the xml serializer to serialize the objects out of a
collection into a file. The object that is being serialized is a
class in my application. When I serialize my class it produces the
following line.

<?xml version="1.0" encoding="utf-16"?>

for each instance of my serialized class. Thus giving me the
following output.

<?xml version="1.0" encoding="utf-16"?>
<Message>
....
</Message>
<?xml version="1.0" encoding="utf-16"?>
<Message>
....
</Message>

I just want the following
<Message>
....
</Message>
<Message>
....
</Message>

as I will be adding some of my own formatting to the xml file when I
write it out to a file. So my questions are
[1] Can I remove the <?xml version="1.0" encoding="utf-16"?> line?
[2] If I can't remove it does it really matter that its in thier?

Amy.
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Amy L. wrote:
[2] If I can't remove it does it really matter that its in thier?


Well, basically you can avoid it, but why?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2

P: n/a
yes, you can remove it. And yes, it matters that it is in there. The
start-of-document should appear only... uh.... at the start of the XML
document.

To remove it, you need to provide your own implementation of a
System.Xml.XmlTextWriter, and override the WriteStartDocument() method, like
so:

public class XmlTextWriterFormattedNoDeclaration :
System.Xml.XmlTextWriter {
public XmlTextWriterFormattedNoDeclaration (System.IO.TextWriter w) :
base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}
Then, serialize using that XmlTextWriter, for example:

XmlSerializer s= new XmlSerializer(typeof(Message));
s.Serialize(new
XmlTextWriterFormattedNoDeclaration(System.Console .Out), msg);

of course, You can also serialize to a memory stream or whatever. . .

-Dino
"Amy L." <am**@paxemail.com> wrote in message
news:c7**************************@posting.google.c om...
I am using the xml serializer to serialize the objects out of a
collection into a file. The object that is being serialized is a
class in my application. When I serialize my class it produces the
following line.

<?xml version="1.0" encoding="utf-16"?>

for each instance of my serialized class. Thus giving me the
following output.

<?xml version="1.0" encoding="utf-16"?>
<Message>
...
</Message>
<?xml version="1.0" encoding="utf-16"?>
<Message>
...
</Message>

I just want the following
<Message>
...
</Message>
<Message>
...
</Message>

as I will be adding some of my own formatting to the xml file when I
write it out to a file. So my questions are
[1] Can I remove the <?xml version="1.0" encoding="utf-16"?> line?
[2] If I can't remove it does it really matter that its in thier?

Amy.

Nov 12 '05 #3

P: n/a
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<OG*************@TK2MSFTNGP12.phx.gbl>...
Amy L. wrote:
[2] If I can't remove it does it really matter that its in thier?


Well, basically you can avoid it, but why?

Because I am serializing a collection of objects I just want the
object represented in XML without any of the other stuff like "<?xml
version="1.0" encoding="utf-16"?>".

ie.
<Message>
....
</Message>

Than when I get ready to write it out to a file I am going to add some
additional XML to it. Example:

<AllMessages>
<Message>
....
</Message>
</AllMessages>

Is this not the right approach. All I want to do is save each object
in my collection out as XML to a text file to read the objects back in
later. Any thoughts?

Amy.
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.