[C#]在XMLDocument中添加XSL引用

Bha*_*kar 1 xml xslt c#-2.0

我正在从我的C#代码创建一个XML文档.我需要在我的XML文档中添加XSL引用.我的代码是:

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);
Run Code Online (Sandbox Code Playgroud)

我需要<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>在我的XML文档中添加以下行.我该怎么做?通过谷歌找不到多少.

Dar*_*rov 7

试试这个:

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