[英]How to get resumptiontoken value from given xml
我從OAI-PMH請求中獲取XML,其中包含恢復令牌以獲取下一組記錄。 如何獲取恢復令牌Value及其其他屬性,例如completeListSize等。
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="static/style.xsl"?><OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2015-06-24T16:45:25Z</responseDate>
<request verb="ListRecords" metadataPrefix="uketd_dc">http://publications.iadb.org/oai/request</request>
<ListRecords>
<record>
<header>
<identifier>oai:publications.iadb.org:11319/195</identifier>
<datestamp>2015-06-12T23:02:48Z</datestamp>
<setSpec>com_123456789_1</setSpec>
<setSpec>col_123456789_3</setSpec>
</header>
<metadata></metadata>
</record>
<resumptionToken completeListSize="6305" cursor="0">MToxMDB8Mjp8Mzp8NDp8NTp1a2V0ZF9kYw==</resumptionToken>
</ListRecords>
我嘗試了以下代碼
XDocument root= XDocument .Load("http://publications.iadb.org/oai/request?verb=ListRecords&metadataPrefix=uketd_dc");
var tokenValue= root.Element("resumptionToken").Value;
返回對象引用錯誤。請幫忙。
Element
返回直接子元素。 從文檔的上下文來看,唯一可用的元素是OAI-PMH
這就是為什么您會得到null引用異常的原因。
此外,你的目標元素有一個命名空間由默認命名空間聲明(定義xmlns="..."
根) OAI-PMH
元。
您可以使用Descendants
查找具有所需名稱的文檔中的任何元素,因此此簡短代碼應適用:
XNamespace ns = "http://www.openarchives.org/OAI/2.0/";
var tokenValue = (string)root.Descendants(ns + "resumptionToken").Single();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.