XmlSerializer with a specific style reference

G

Guest

I have a data structure that I am writing to an XML file using XmlSerializer.

XmlSerializer xSer = new XmlSerializer(typeof(MyData));
StreamWriter sw = new StreamWriter("SomeData.xml");
xSer.Serialize(sw, theData);
sw.Close();

I need to add a style reference within the file that looks like this:
<?xml-stylesheet type="text/xsl" href="MyStyle.xsl"?>

Can someone show me how this is done? Can it be done with the XmlSerializer
class? I can't seem to find the method.
 
W

Wiebe Tijsma

Hi Steve,

instead of using a StreamWriter, you could use a XmlTextWriter (not 100%
sure if this usage of WriteProcessingInstruction() is valid)

XmlSerializer xSer = new XmlSerializer(typeof(MyData));

using(StreamWriter streamWriter = new StreamWriter("SomeData.xml")){
using(XmlTextWriter xmlWriter = new XmlTextWriter(streamWriter)){

xmlWriter.WriteProcessingInstruction(
"xml-stylesheet",
"type=\"text/xsl\" href=\"MyStyle.xsl\"");

xSer.Serialize(xmlWriter, theData);
}
}

Or look at this to see how you can include PI's in your MyData object:

http://www.topxml.com/xmlserializer/serializing_xml_nodes.asp

Regards,

Wiebe Tijsma
 
G

Guest

This solution looks wonderful and simple to implement. I'll try it out
immediately. Thanks so much!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top