gpt4 book ai didi

c# - 如何从 节点中获取 href 属性值?

转载 作者:数据小太阳 更新时间:2023-10-29 01:43:13 30 4
gpt4 key购买 nike

我们从一家供应商那里获得了一份 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/

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