【问题标题】:read xml with multiple namespaces读取具有多个命名空间的 xml
【发布时间】:2015-06-26 01:12:24
【问题描述】:

我正在努力获得

<dc:identifier xsi:type="dcterms:URI" >bitstream link</dc:identifier>

和 dc:title、publisher 等在我的 MVC 应用程序中使用 C# 需要帮助,因为我是 xml 新手

<metadata>
  <uketd_dc:uketddc
         xmlns:uketd_dc="http://naca.central.cranfield.ac.uk/ethos-oai/2.0/"
         xmlns:doc="http://www.lyncode.com/xoai"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:uketdterms="http://naca.central.cranfield.ac.uk/ethos-oai/terms/" 
         xmlns:dcterms="http://purl.org/dc/terms/"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xsi:schemaLocation="http://naca.central.cranfield.ac.uk/ethos-oai/2.0/ http://naca.central.cranfield.ac.uk/ethos-oai/2.0/uketd_dc.xsd">
    <dc:date>2010-10-14T17:31:59Z</dc:date>
    <dc:date>2010-10-14T17:31:59Z</dc:date>
    <dc:date>2003-01-01</dc:date>
    <dc:date>2015-06-12T10:34:09Z</dc:date>
    <dcterms:issued>2003-01-01</dcterms:issued>
    <dcterms:isReferencedBy xsi:type="dcterms:URI">http://www.iadb.org/en/publications/publication-detail,7101.html?id=4486</dcterms:isReferencedBy>
    <dcterms:abstract>This book looks at the key issues and lessons that policymakers must consider in designing an adequate framework for dealing with financial crises. These include structural problems and their causes, policy actions, the role of market discipline, and preemptive strategies.</dcterms:abstract>
    <dc:title>Financial Crises in Japan and Latin America</dc:title>
    <dc:creator>Zahler, Roberto</dc:creator>
    <dc:contributor>VICTORCO</dc:contributor>
    <dc:subject>Financial Crises &amp; Economic Stabilization</dc:subject>
    <dc:description>This book looks at the key issues and lessons that policymakers must consider in designing an adequate framework for dealing with financial crises. These include structural problems and their causes, policy actions, the role of market discipline, and preemptive strategies.</dc:description>
    <dc:type>Books</dc:type>
    <dc:identifier>International Lending and Debt Problems</dc:identifier>
    <dc:identifier>International Lending and Debt Problems</dc:identifier>
    <dc:identifier>International Lending and Debt Problems</dc:identifier>
    <dc:identifier>International Lending and Debt Problems</dc:identifier>
    <dc:identifier>International Lending and Debt Problems</dc:identifier>
    <dc:identifier>9781931003476</dc:identifier>
    <dc:identifier>http://www.iadb.org/en/publications/publication-detail,7101.html?id=4486</dc:identifier>
    <dc:language>en</dc:language>
    <dc:publisher>Inter-American Development Bank (IDB)</dc:publisher>
    <dc:identifier xsi:type="dcterms:URI">http://publications.iadb.org/bitstream/handle/11319/195/Financial+Crises+in+Japan+and+Latin+America.pdf?sequence=1</dc:identifier>
    <uketdterms:checksum xsi:type="uketdterms:MD5">83c42636d27d253499be4db09db02312</uketdterms:checksum>
    <dc:identifier xsi:type="dcterms:URI">http://publications.iadb.org/bitstream/handle/11319/195/Financial+Crises+in+Japan+and+Latin+America.pdf.png?sequence=11</dc:identifier>
    <uketdterms:checksum xsi:type="uketdterms:MD5">4d4c1c29a730054db0cfcfc477333626</uketdterms:checksum>
    <dc:identifier xsi:type="dcterms:URI">http://publications.iadb.org/bitstream/handle/11319/195/Financial+Crises+in+Japan+and+Latin+America.pdf.jpg?sequence=5</dc:identifier>
    <uketdterms:checksum xsi:type="uketdterms:MD5">7e49ce2e1c21c12e141aa50402037b25</uketdterms:checksum>
    <dc:identifier xsi:type="dcterms:URI">http://publications.iadb.org/bitstream/handle/11319/195/Financial+Crises+in+Japan+and+Latin+America.pdf.txt?sequence=10</dc:identifier>
    <uketdterms:checksum xsi:type="uketdterms:MD5">bad0c21c28fc8a9946bdfa5eae2bf59d</uketdterms:checksum>
  </uketd_dc:uketddc>
</metadata>

我尝试了以下代码,但无法获取详细信息

XDocument billingData = XDocument.Load("http://publications.iadb.org/oai/request?verb=ListRecords&metadataPrefix=uketd_dc");
XNamespace oai_dc = XNamespace.Get("http://naca.central.cranfield.ac.uk/ethos-oai/2.0/");
XNamespace oai_dc1 = XNamespace.Get("http://naca.central.cranfield.ac.uk/ethos-oai/2.0/ http://naca.central.cranfield.ac.uk/ethos-oai/2.0/uketd_dc.xsd");
XNamespace dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");

var billings = from billing in billingData.Descendants(oai_dc + "uketd_dc").Descendants(oai_dc1 + "dc")
               select new Billing(billing.Element(dc + "title").Value, billing.Element(dc + "creator").Value,
                       billing.Element(dc + "description").Value, billing.Element(dc + "identifier").Value);
allBillings.AddRange(billings.ToList<Billing>());

我不知道我做得对不对。

【问题讨论】:

  • 我已经发布了我在上面尝试过的代码..请检查
  • "无法获取详细信息" 为什么?你得到一个错误?结果错误?究竟是哪个?顺便说一句,如果您发布了清晰且缩进良好的代码,这将有助于我们为您提供帮助。
  • 从上面给出的 xml 我试图获取包含 .pdf 链接的标题、贡献者、发布者和标识符。在尝试上面的代码时,它显示枚举没有产生任何结果。我怎样才能从上面的 xml 中得到这些东西..

标签: c# xml asp.net-mvc-4


【解决方案1】:

您的查询正在查找元素本地名称为 uketd_dcdc 的后代。您没有任何具有这些名称的元素 - 这些似乎是名称空间前缀。另外,我不确定您的 oai_dc1 命名空间应该是什么。

你可能想要的是这个:

XNamespace uketd_dc = "http://naca.central.cranfield.ac.uk/ethos-oai/2.0/";
XNamespace dc = "http://purl.org/dc/elements/1.1/";

var billings = from e in doc.Descendants(uketd_dc + "uketddc")
               select new Billing(
                    (string)e.Element(dc + "title"),
                    (string)e.Element(dc + "creator"),
                    (string)e.Element(dc + "description"),
                    (string)e.Element(dc + "identifier")
                    ); 

请注意,您的 XML 有多个元素用于其中的一些(例如标识符)。这将返回第一个。

【讨论】:

  • 还有一个疑问,实际上我是从 OAI-PMH 获取 xml,它实际上包含 1000 条记录,但我只从 XDocument billingData2 = XDocument.Load("repository.iadb.org/oai/… ); 我可以在一个请求中获取所有记录吗?
  • 它会得到所有这些。如果它只返回 100,那么源中只有 100。我会指出,您问题中的链接返回的 XML 中有 100 个。您评论中的链接不同,并返回具有不同命名空间的不同 XML - 其中只有 87 条记录。
  • XDocument billingData = XDocument.Load("publications.iadb.org/oai/…); 在 xml 的末尾显示类似这样的内容 MToxMDB8Mjp8Mzp8NDp8NTp1a2V0ZF9kYw== 但是在直接访问链接时,它显示总共 6306 条记录...
  • 如上所述,xml 有多个元素用于其中一些(例如标识符)。如何检索具有 pdf 和 png 链接的标识符 国际借贷和债务问题:标识符>publications.iadb.org/bitstream/handle/11319/195/…> publications.iadb.org/bitstream/handle/11319/195/…>
  • 您必须找到某种方法来识别它们并使用Where 过滤标识符元素。我看不出如何根据 uri 判断它是 png 还是 pdf。
猜你喜欢
  • 2018-06-03
  • 1970-01-01
  • 2017-04-29
  • 2014-02-07
  • 2014-06-14
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多