Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amy L. - 07 Sep 2004 04:32 GMT
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.
Oleg Tkachenko [MVP] - 07 Sep 2004 09:56 GMT
> [2] If I can't remove it does it really matter that its in thier?

Well, basically you can avoid it, but why?

Signature

Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Amy L. - 07 Sep 2004 20:00 GMT
> > [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.
Dino Chiesa [Microsoft] - 07 Sep 2004 17:17 GMT
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

> 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
[quoted text clipped - 29 lines]
>
> Amy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.