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

2

11

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

Choanocyte answered 27/1, 2010 at 9:36 Comment(0)
U
21

XmlDocument.CreateProcessingInstruction Method

public static void Main()
{
    var doc = new XmlDocument();
    doc.AppendChild(doc.CreateProcessingInstruction(
        "xml-stylesheet", 
        "type='text/xsl' href='colors.xslt'"));
}
Ultranationalism answered 27/1, 2010 at 9:40 Comment(0)
M
0

For LINQ XDocument you can use this:

    XDocument xDoc = new XDocument();
    xDoc.Add(new XProcessingInstruction("xml-stylesheet", 
                                        "type=\"text/xsl\" href=\"xml-style.xslt\""));
Malissa answered 10/12, 2019 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.