469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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 13422
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.