[C#]Add XSL reference in XMLDocument

c#-2.0, xml, xslt

Solution

Try this:

var xDoc = new XmlDocument();
var pi = xDoc.CreateProcessingInstruction(
    "xml-stylesheet", 
    "type=\"text/xsl\" href=\"cdcatalog.xsl\"");
xDoc.AppendChild(pi);

Problem

I am creating an XML document from my C# code. I need to add the XSL reference in my XML document. My code is: ``` XmlDocument xDoc = new XmlDocument(); if (!File.Exists(fileName)) { XmlDeclaration dec = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xDoc.AppendChild(dec); **[Need to add code to add the XSL reference e.g. - <?xml-stylesheet type="text/xsl" href="style.xsl"?> ] ** XmlElement root = xDoc.CreateElement("Errors"); xDoc.AppendChild(root); } else { xDoc.Load(fileName); } XmlElement errorLogStart = xDoc.CreateElement("ErrorLog"); XmlElement errorText = xDoc.CreateElement("Message"); errorText.InnerText = message; errorLogStart.AppendChild(errorText); xDoc.DocumentElement.InsertBefore(errorLogStart, xDoc.DocumentElement.FirstChild); FileStream fileXml = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); xDoc.Save(fileXml); ``` I need to add the following line - `<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>` in my XML document. How can I do it? Couldn't find much through google.

Original source