Serialize a class adding a stylesheet

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to be able to serialize my class such that the XML file contains a
stylesheet directive after the XML definition as below:

<?xml version="1.0" ?>
<xml-stylesheet type="test/xsl" href="abc.xsl"?>
<root>
etc
etc

Can anyone tell me how to do this please?

Pazza.
 
If you serialize a class, only class members will be serialized. As an XSL
stylesheet is a string, you have the option of creating a member of the
class (which represents the stylesheet) that can be serialized with it.
Otherswise, use an external stylesheet.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
Kevin,

Thanks for your reply.

I created a schema and generated the class using xsd.exe.
I then created an instance of the class and serialized it out to a file.
The result was the following:

<?xml version="1.0" encoding="utf-8"?>
<className xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<stylesheet type="text/xsl" href="stylesheets/abc.xsl" />
<Header>
etc
etc

I have two problems with this ...
(1) The <stylesheet> element is not formed properly ie. it is missing the ?
from the start and end. And
(2) I want the <stylesheet> element to appear before the root elment
<className>.

Can you offer any advice on how to correct (1) and how to achive (2) please?

Pazza
 
I can give it another shot. First, if you serialize a class, the class name
element is going to be the root element of the serialized class. Now, you
can certainly create an XML file from the class, but if you want the
stylesheet to appear before the class element, it can not be a member of the
class, but would have to be inserted into the XML file. What exactly is the
requirement you are trying to fulfill here? Because if serializing a class
is the requirement, you're barking up the wrong tree. But if creating an XML
document from a serialized class is your goal, that is certainly doable. The
XML document will not *be* the serialized class, but may *contain* the
serialized class.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 
Kevin,

I want to create an XML document from my object. This XML document will then
form the input into another application and at the same time be viewable via
IE using the stylesheet.

Pazza.
 
Here is how you should be able to achieve this:

Serialize your object to a string instead of serializing your object
directly to a file. You can use the Serialize(TextWriter, object)
overload of the XMLSerializer using a StringWriter as the first
parameter (StringWriter derives from TextWriter).

The StringWriter class writes its information to an underlying
StringBuilder that can be accessed by calling the GetStringBuilder()
method of the StringWriter.

Create an empty XmlDocument object and load Xml from the StringBuilder.

Use the CreateProcessingInstruction method to create your stylesheet
declaration.

Call the XmlDocument.InsertBefore method to insert your stylesheet
declaration before the root node.

Save the XmlDocument to a file.

I haven't tried this out, but I think it should work. Let me know if
you have any problems.

Regards,
Sarin.
 
Yes, assuming that creating an XML document is the requirement, something
like this is what I would recommend.

Another, and perhaps more elegant solution is to serialize the class as an
XML document, and use XSLT to transform it with the additional code into an
XML document that has the additional code (style sheet, etc). This separates
the transform from the serialized class nicely, and enables you to perform
different transforms for different uses.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 
Back
Top