I am trying to read to some data in XML format which is CDATA in my windows 8 phone app. Here is a sample of the data:
我试图在我的Windows 8手机应用程序中读取XML格式的一些数据,这是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>
I want to get a data structure which contains the ChapterName, ChapterID and a List of all the verse contents and their corresponding VerseIDs. Please note that by verse content, I mean the CDATA. I need to use XDocument but I cannot figure out how to parse this complex XML.
我想获得一个数据结构,其中包含ChapterName,ChapterID和所有经文内容的列表及其对应的VerseID。请注意,根据诗歌内容,我的意思是CDATA。我需要使用XDocument,但我无法弄清楚如何解析这个复杂的XML。
I will greatly appreciate any help!
我将非常感谢任何帮助!
Thanks!
1 个解决方案
#1
0
First, you need a Chapter
and a Verse
class:
首先,你需要一个章节和一个诗歌类:
public class Chapter
{
public int Id { get; set; }
public string Name { get; set; }
public List<Verse> Verses { get; set;}
}
public class Verse
{
public int Id { get; set; }
public string Contents { get; set; }
public static Verse FromXElement(XElement element)
{
return new Verse { Id = (int)element.Attribute("VerseID"),
Contents = element.Value };
}
}
Then it's simpler:
然后它更简单:
XDocument doc = //get your document
XElement chapter = doc.Root.Element("Chapter");
Chapter result = new Chapter { Id = (int)chapter.Attribute("ChapterId"),
Name = (string)chapter.Attribute("ChapterName"),
Verses = chapter.Elements().Select(Verse.FromXElement).ToList() }
(I put the FromXElement
method because it looks neater, you can inline it if you want though)
(我把FromXElement方法看起来更整洁,如果你想要你可以内联它)
#1
0
First, you need a Chapter
and a Verse
class:
首先,你需要一个章节和一个诗歌类:
public class Chapter
{
public int Id { get; set; }
public string Name { get; set; }
public List<Verse> Verses { get; set;}
}
public class Verse
{
public int Id { get; set; }
public string Contents { get; set; }
public static Verse FromXElement(XElement element)
{
return new Verse { Id = (int)element.Attribute("VerseID"),
Contents = element.Value };
}
}
Then it's simpler:
然后它更简单:
XDocument doc = //get your document
XElement chapter = doc.Root.Element("Chapter");
Chapter result = new Chapter { Id = (int)chapter.Attribute("ChapterId"),
Name = (string)chapter.Attribute("ChapterName"),
Verses = chapter.Elements().Select(Verse.FromXElement).ToList() }
(I put the FromXElement
method because it looks neater, you can inline it if you want though)
(我把FromXElement方法看起来更整洁,如果你想要你可以内联它)