Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add xml-stylesheet tags to an XML file using C#?

Tags:

c#

xml

xslt

I need to add the following code to the beginning of an XML file, while creating it:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="colors.xslt"?>

I'm sure there is a method for this, but I haven't found it yet. I'm using C#. Thank you

like image 892
Bogdan Craciun Avatar asked Jan 27 '10 09:01

Bogdan Craciun


People also ask

How do I create a stylesheet in XML?

Basic steps in defining a CSS style sheet for XML :Define the style rules for the text elements such as font-size, color, font-weight, etc. Define each element either as a block, inline or list element, using the display property of CSS. Identify the titles and bold them.

How do you link an XSLT stylesheet to an XML document?

Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <? xml-stylesheet type='text/xsl' href='filename.

Which class is used in C for writing an XML file?

XmlSerializer Class (System. Xml. Serialization) Serializes and deserializes objects into and from XML documents.


1 Answers

XmlDocument.CreateProcessingInstruction Method

public static void Main()
{
    var doc = new XmlDocument();
    doc.AppendChild(doc.CreateProcessingInstruction(
        "xml-stylesheet", 
        "type='text/xsl' href='colors.xslt'"));
}
like image 133
Rubens Farias Avatar answered Oct 09 '22 13:10

Rubens Farias