473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2617
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.XmlT extWriter, and override the WriteStartDocum ent() method, like
so:

public class XmlTextWriterFo rmattedNoDeclar ation :
System.Xml.XmlT extWriter {
public XmlTextWriterFo rmattedNoDeclar ation (System.IO.Text Writer w) :
base(w) { Formatting= System.Xml.Form atting.Indented ;}
public override void WriteStartDocum ent () { }
}
Then, serialize using that XmlTextWriter, for example:

XmlSerializer s= new XmlSerializer(t ypeof(Message)) ;
s.Serialize(new
XmlTextWriterFo rmattedNoDeclar ation(System.Co nsole.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.goo gle.com...
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!P LEASEtkachenko. com> wrote in message news:<OG******* ******@TK2MSFTN GP12.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
2517
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
4792
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 = typeof(System.String) as <type>System.String</type> thanks a ton.
2
3039
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 XmlTextWriter do this?? Thanks. Amil
10
8312
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) This way I have to write another "serializing" function for every new kind of struct I want to write, though. Is there a way to write functions that can write/read any struct to/from plain text format in a portable way?
9
2310
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 that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
11
41094
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 debug_print_backtrace in the file to see how it is included, and here's the output: #0 require_once() called at #1 require_once(\eKirje.textGrid.class.php) called at #0 require_once() called at #1 require_once(\eKirje.kanava.class.php)...
2
1356
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 nor give any guidance. It probably depends on the function itself so are you supposed to determine this. Thanks.
1
10521
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
3681
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
9423
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
10222
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
10050
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
9999
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
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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...
3
2815
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.