gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:25:32 24 4
gpt4 key购买 nike

我正在从我的 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-stylesheet type="text/xsl" href="cdcatalog.xsl"?>在我的 XML 文档中。我该怎么做?通过谷歌找不到太多。

最佳答案

试试这个:

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

关于xml - [C#]在 XMLDocument 中添加 XSL 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1845699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com