gpt4 book ai didi

c# - 如何使用 LINQ 读取包含 xincluded 文件的 XML?

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:31 25 4
gpt4 key购买 nike

我目前正在使用 VS2008、C# 3.5 和 LINQ 来读取 XML 文件。 (称之为 A.xml)我想使用 xinclude 将其他 XML 文件包含在 A.xml 中。我最初的尝试失败了,而且我还没有找到关于这两者如何协同工作的任何现有信息。

我正在通过调用 XElement.Load() 创建查询的根元素有没有其他方法可以创建将处理 include 语句的根元素?

例子:
文件A.xml

<A>  
<xi:include href="B.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
</A>

文件B.xml

<B>  
<TAG_B Name="foo">
<TAG_C attr="bar"/>
</TAG_B>
</B>

C#代码:

 XElement root = XElement.Load(@"A.xml");
var b = from TAG_B in root.Descendants("B")
let name = (string)TAG_B.Attribute("Name")
where name.Equals("foo")
select TAG_B;

我希望 XElement.Load 包含 B.xml 的内容并产生以下内容:

<A> 
<B>
<TAG_B Name="foo">
<TAG_C attr="bar"/>
</TAG_B>
</B>
</A>

不幸的是,include元素没有被处理,所以enumarable中没有TAG_B元素。

最佳答案

LINQ 和 .Net 框架不支持 XInclude。 SourceForge 有一个免费项目为 .Net 实现 XInclude,但我认为它不支持 LINQ。

关于c# - 如何使用 LINQ 读取包含 xincluded 文件的 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2275923/

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