读取具有多个名称空间的xml [英] read xml with multiple namespaces

查看:88
本文介绍了读取具有多个名称空间的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试

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

在我的MVC应用程序中使用C#的

和dc:title,publisher等需要帮助,因为我是xml的新手

and dc:title,publisher etc using C# in my MVC app need help as i am new to 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>

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

I tried the below code and not able to get the details

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

我不知道我做的是否正确.

I don't know whether i am doing correct.

推荐答案

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

Your query is looking for descendants with the element local names of uketd_dc and then dc. You don't have any elements with those names - these seem to be namespace prefixes. Also, I'm not sure what your oai_dc1 namespace is supposed to be.

您可能想要的是这样:

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对其中某些元素具有多个元素(例如,标识符).这将返回第一个.

Note that your XML has multiple elements for some of these (e.g. identifier). This will return the first one.

这篇关于读取具有多个名称空间的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆