472,129 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

DOCTYPE with Xml Serialization

I have a class that serialize using the XmlSerializer.
I am using several attributes for class members to control serialization
process.
One thing I cannot figure out how to do is how to add DOCTYPE attribute to
the file.
This XML file must use the doctype that refers to the DTD rather than an
XSD.

Could someone point out how to accomplish that using the attributes or by
some other means?

Thanks!
Nov 12 '05 #1
3 14190
Hi Vlad,

Welcome to MSDN newsgroup.
From your description, when you use the .NET XmlSerizliation to serizliaze
some class instances out to xml stream, you want to also expose the DocType
for the whole xml document, yes?

As for the DOCTYPE, there hasn't buildin XmlSerlization Attributes for
specifying DOCTYPE. In fact, this is because the Xml Serialization is
Class/Element based rather than Document based. As for DOCTYPE, it is
specified for a certain Xml document. Currently I think we can use the
following means to inject the DOCTYPE in our serilization's output document:

When do the serizliation, we use an XmlTextWriter to delegate the
underlying StreamWriter we'll write out the serialized instances. And we
can use the XmlWriter.WriteDocType to inject our custom DocType. For
example:

XmlSerializer serializer = new XmlSerializer(typeof(Group));

Group group = new Group();
StreamWriter sw = new StreamWriter("group.xml");
XmlWriter xw = new XmlTextWriter(sw);

xw.WriteDocType("Group", null, null, "<!ENTITY h 'hardcover'>");

serializer.Serialize(xw,group);

xw.Close();
sw.Close();

HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 12 '05 #2
Thanks a lot Steven! I'll try it.
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Oi*************@TK2MSFTNGXA02.phx.gbl...
Hi Vlad,

Welcome to MSDN newsgroup.
From your description, when you use the .NET XmlSerizliation to serizliaze
some class instances out to xml stream, you want to also expose the DocType for the whole xml document, yes?

As for the DOCTYPE, there hasn't buildin XmlSerlization Attributes for
specifying DOCTYPE. In fact, this is because the Xml Serialization is
Class/Element based rather than Document based. As for DOCTYPE, it is
specified for a certain Xml document. Currently I think we can use the
following means to inject the DOCTYPE in our serilization's output document:
When do the serizliation, we use an XmlTextWriter to delegate the
underlying StreamWriter we'll write out the serialized instances. And we
can use the XmlWriter.WriteDocType to inject our custom DocType. For
example:

XmlSerializer serializer = new XmlSerializer(typeof(Group));

Group group = new Group();
StreamWriter sw = new StreamWriter("group.xml");
XmlWriter xw = new XmlTextWriter(sw);

xw.WriteDocType("Group", null, null, "<!ENTITY h 'hardcover'>");

serializer.Serialize(xw,group);

xw.Close();
sw.Close();

HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 12 '05 #3
You're welcome Vlad,

Good Luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Vlad | last post: by
3 posts views Thread by Aaron Clamage | last post: by
20 posts views Thread by Deborah | last post: by
4 posts views Thread by mijalko | last post: by
5 posts views Thread by Nikola Skoric | last post: by

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.