在Windows 8手机应用程序中阅读XML CDATA

时间:2013-11-22 22:07:33

标签: c# xml windows-phone-8 linq-to-xml cdata

我正在尝试读取XML格式的一些数据,这是我的Windows 8手机应用程序中的CDATA。以下是数据样本:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE HolyQuran [
<!ATTLIST HolyQuran TranslationID CDATA #REQUIRED>
<!ATTLIST HolyQuran Writer CDATA #REQUIRED>
<!ATTLIST HolyQuran Language CDATA #REQUIRED>
<!ATTLIST HolyQuran LanguageIsoCode CDATA #REQUIRED>
<!ATTLIST HolyQuran Direction (rtl|ltr) #REQUIRED>
<!ELEMENT HolyQuran (Chapter+)>
<!ATTLIST Chapter ChapterID CDATA #REQUIRED>
<!ATTLIST Chapter ChapterName CDATA #REQUIRED>
<!ELEMENT Chapter (Verse+)>
<!ATTLIST Verse VerseID CDATA #REQUIRED>
<!ELEMENT Verse (#PCDATA)>
  ]>
<!-- This SQL Query Generated at 22 November 2013 01:44 (UTC) from
  www.qurandatabase.org -->
<HolyQuran TranslationID="59" Writer="Yusuf Ali" Language="English"
LanguageIsoCode="eng" Direction="ltr">
<Chapter ChapterID="1" ChapterName="The Opening">
    <Verse VerseID="1"><![CDATA[In the name of Allah, Most Gracious, Most
                                  Merciful.]]></Verse>
    <Verse VerseID="2"><![CDATA[Praise be to Allah, the Cherisher and Sustainer
                                  of the worlds;]]></Verse>
    <Verse VerseID="3"><![CDATA[Most Gracious, Most Merciful;]]></Verse>
    <Verse VerseID="4"><![CDATA[Master of the Day of Judgment.]]></Verse>
    <Verse VerseID="5"><![CDATA[Thee do we worship, and Thine aid we seek.
                                 ]]></Verse>
    <Verse VerseID="6"><![CDATA[Show us the straight way,]]></Verse>
    <Verse VerseID="7"><![CDATA[The way of those on whom Thou hast bestowed Thy
                                 Grace, those whose (portion) is not wrath, and who go
                                 not astray.]]></Verse>
</Chapter>
</HolyQuran>

我想获得所有CDATA以及相应的VerseID。我需要在Windows 8 Phone应用程序中执行此操作。任何人都可以告诉我如何正确获得此CDATA?我正在尝试Xdocument,但我没有多少运气。

谢谢!

1 个答案:

答案 0 :(得分:0)

没有太多运气没有说明你对XDocument的问题,但我认为这仍然是最好的方式:

var xDoc = XDocument.Load("Input.xml");

var items = xDoc.Root
                .Element("Chapter")
                .Elements("Verse")
                .Select(v => new
                {
                    Id = (int)v.Attribute("VerseID"),
                    Content = (string)v
                }).ToList();

items是一个包含两个属性的匿名类型对象列表:Id包含VerseID值,Content包含CDATA内容。