473,388 Members | 1,277 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,388 software developers and data experts.

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

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
3 2584
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
3
by: Maersa | last post by:
Hi All, Was anybody able to serialize the "Type" class properly using XmlSerializer().... Want to serialize and deserialze the Type in a string form, but can this be done ? myobj.Type =...
2
by: Amil Hanish | last post by:
I am trying to use XmlTextWriter. I write the xml data fine, but the file is missing the first line that shows the version and encoding: <?xml version="1.0" encoding="utf-8"?> Can the...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
11
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
2
by: Jim Matheson | last post by:
Hi there, When overriding form/control functions like "OnLoad()", "OnFormClosed()", etc., should the base class version be called before or after your own code. The docs say nothing about this...
1
by: Martin Plechsmid | last post by:
Hello, (After investigating my real problem) I've got a C# web service containing a single method: public MyBDO Test() { return null; }
8
by: cj | last post by:
I've seen examples of web services written with <%@ WebService Language ="Vb" Class=".... at the top. Is it not used in VB 2008?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...
0
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...

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.