作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我们从一家供应商那里获得了一份 XML 文档,我们需要使用他们的样式表执行 XSL 转换,以便我们可以将生成的 HTML 转换为 PDF。实际样式表在 XML 文档中 ?xml-stylesheet
定义的 href
属性中引用。有什么方法可以使用 C# 获取该 URL 吗?我不相信供应商不会更改 URL,并且显然不想对其进行硬编码。
带有完整 ?xml-stylesheet
元素的 XML 文件的开头如下所示:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.fakeurl.com/StyleSheet.xsl"?>
最佳答案
由于处理指令可以包含任何内容,因此它在形式上没有任何属性。但是如果你知道有“伪”属性,比如在 xml-stylesheet 处理指令的情况下,那么你当然可以使用处理指令的值来构造单个元素的标记并使用 XML 解析器解析它:
XmlDocument doc = new XmlDocument();
doc.Load(@"file.xml");
XmlNode pi = doc.SelectSingleNode("processing-instruction('xml-stylesheet')");
if (pi != null)
{
XmlElement piEl = (XmlElement)doc.ReadNode(XmlReader.Create(new StringReader("<pi " + pi.Value + "/>")));
string href = piEl.GetAttribute("href");
Console.WriteLine(href);
}
else
{
Console.WriteLine("No pi found.");
}
关于c# - 如何从 <?xml-stylesheet> 节点中获取 href 属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2119699/
我是一名优秀的程序员,十分优秀!