Xml.Linq:后裔()返回任何内容 [英] Xml.Linq: Descendants() returns nothing
本文介绍了Xml.Linq:后裔()返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从NCX文件读取使用的XElement(即XML文件):
I am trying to read from an ncx file (i.e. xml file) using XElement:
XElement foundNode = ncx.Descendants("navPoint").Where(r => r.Attribute("class").Value == "chapter").FirstOrDefault();
因此,foundNode为null,因为ncx.Descendants(的navPoint)返回一个空的枚举。但数据有:
As a result, foundNode is null because ncx.Descendants("navPoint") returns an empty enumeration. But the data is there:
<?xml version='1.0' encoding='utf-8'?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en">
<head>
<meta content="8eab2efe-d584-478a-8c73-1304d2ae98fa" name="dtb:uid"/>
<meta content="3" name="dtb:depth"/>
<meta content="calibre (0.8.12)" name="dtb:generator"/>
<meta content="0" name="dtb:totalPageCount"/>
<meta content="0" name="dtb:maxPageNumber"/>
</head>
<docTitle>
<text>Fine</text>
</docTitle>
<navMap>
<navPoint class="chapter" id="27665f37-ecf5-4934-a044-4f77152e54d9" playOrder="1">
<navLabel>
<text>I. BLIND</text>
</navLabel>
<content src="Fine_split_003.html"/>
能否请您解释一下什么是错在这里?谢谢
Could you please explain what is wrong here? Thanks.
推荐答案
您需要采取命名空间中的XML考虑:
You need to take namespace in XML into account:
XDocument ncx = XDocument.Load("file.xml");
XNamespace df = ncx.Root.Name.Namespace;
XElement foundNode = ncx.Descendants(df + "navPoint").Where(r => r.Attribute("class").Value == "chapter").FirstOrDefault();
这篇关于Xml.Linq:后裔()返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文