簡體   English   中英

Xml.Linq:Descendants()什么都不返回

[英]Xml.Linq: Descendants() returns nothing

我試圖使用XElement從ncx文件(即xml文件)中讀取:

XElement foundNode = ncx.Descendants("navPoint").Where(r => r.Attribute("class").Value == "chapter").FirstOrDefault();

因此,foundNode為null,因為ncx.Descendants(“navPoint”)返回空枚舉。 但數據是:

<?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"/>

你能解釋一下這里有什么問題嗎? 謝謝。

您需要考慮XML中的命名空間:

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();

您還可以使用XElement.Name.LocalName屬性去除命名空間或引用元素而不使用命名空間: 此處的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM