2012-04-17 98 views
0

我有以下的XML我怎樣才能讀取XML獲得蘇拉的名單像這樣XML在C#.NET解析4


var Suras = XMLNodes **//how to use xpath to load xmlnodes of sura** 

foreach (var sura in suras) 
{ 
    var ayas = sura. **//how to use xpath to load xmlnodes of aya for this sura node** 
} 

XML節點

<?xml version="1.0" encoding="utf-8" ?> 
<quran> 
    <sura index="1" name="الفاتحة"> 
    <aya index="1" text="In the name of Allah, the Entirely Merciful, the Especially Merciful."/> 
    <aya index="2" text="[All] praise is [due] to Allah, Lord of the worlds -"/> 
    </sura> 
    <sura index="114" name="الناس"> 
    <aya index="1" text="Say, &quot;I seek refuge in the Lord of mankind,"/> 
    <aya index="2" text="The Sovereign of mankind."/> 
    <aya index="3" text="The God of mankind,"/> 
    <aya index="4" text="From the evil of the retreating whisperer -"/> 
    <aya index="5" text="Who whispers [evil] into the breasts of mankind -"/> 
    <aya index="6" text="From among the jinn and mankind.&quot;"/> 
    </sura> 
</quran> 

回答

2

特別是想使用XPath嗎?我只是使用LINQ to XML:

XDocument doc = XDocument.Load("file.xml"); 
var suras = doc.Root.Elements("sura"); 
foreach (var sura in suras) 
{ 
    var ayas = suras.Elements("aya"); 
    ... 
} 
+0

是的,希望不使用LINQ來使用XML。所以它會在.net 4&2上運行。 – 2012-04-17 09:16:51

+5

@SOFUser:那你爲什麼把你的問題標記爲「c#-4.0」? – 2012-04-17 12:51:10