【问题标题】:[C#]Add XSL reference in XMLDocument[C#]在 XMLDocument 中添加 XSL 引用
【发布时间】:2009-12-04 08:51:02
【问题描述】:

我正在从我的 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);

我需要在我的 XML 文档中添加以下行 - &lt;?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?&gt;。我该怎么做?通过谷歌找不到很多东西。

【问题讨论】:

    标签: xml xslt c#-2.0


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多