2015-06-25 105 views
0

我試圖讓讀取XML和多個命名空間

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

和DC:使用等C#在我的MVC應用程序需要幫助,因爲我是新來的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>()); 

我不知道我是否做得正確。

+0

我已經發布了代碼,我嘗試了頂部..請檢查 –

+0

「*並且無法獲取詳細信息*」爲什麼?你得到一個錯誤?錯誤的結果?究竟是什麼?順便說一下,如果你發佈了清晰且縮進的代碼,這將幫助我們幫助你。 –

+0

從上面給出的XML我試圖得到標題,貢獻者,出版商和包含.pdf鏈接的標識符..在上面的代碼嘗試顯示枚舉沒有結果。我怎麼能從上面的XML獲取這些東西.. –

回答

1

您的查詢正在尋找元素本地名稱爲uketd_dc,然後dc的子孫。你沒有任何這些名稱的元素 - 這些似乎是名稱空間前綴。另外,我不確定你的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有一些這樣的多個元素(例如標識符)。這將返回第一個。

+0

另一個疑問其實我從OAI-PMH得到xml它實際上包含1000條記錄,但我只從下面的代碼獲取100條記錄XDocument billingData2 = XDocument.Load ( 「http://repository.iadb.org/oai/request?verb=ListRecords&metadataPrefix=oai_dc」);我可以在一個請求中獲取所有記錄嗎? –

+0

它會得到所有人。如果它只返回100,那麼源中只有100個。我會指出在你的問題中鏈接返回的XML有100個。註釋中的鏈接不同,並返回具有不同名稱空間的不同XML - 這裏只有87條記錄。 –

+0

XDocument billingData = XDocument.Load(「http://publications.iadb.org/oai/request?verb=ListRecords&metadataPrefix=uketd_dc」);在xml的結尾顯示類似這樣的 MToxMDB8Mjp8Mzp8NDp8NTp1a2V0ZF9kYw ==但是直接訪問鏈接時,它顯示總共6306條記錄... –