简体   繁体   中英

how to read a specific xml element in windows 8 platform

I have a xml file of this type

<HolyQuran TranslationID="103" Writer="Maulana Mohammad Ali" Language="English" LanguageIsoCode="eng" Direction="ltr">
<Chapter ChapterID="1" ChapterName="The Opening">
    <Verse VerseID="1"><![CDATA[In the name of Allah, the Beneficent, the Merciful.]]></Verse>
    <Verse VerseID="2"><![CDATA[Praise be to Allah, the Lord of the worlds,]]></Verse>
    <Verse VerseID="3"><![CDATA[The Beneficent, the Merciful,]]></Verse>
    <Verse VerseID="4"><![CDATA[Master of the day of Requital.]]></Verse>
    <Verse VerseID="5"><![CDATA[Thee do we serve and Thee do we beseech for help.]]></Verse>
    <Verse VerseID="6"><![CDATA[Guide us on the right path,]]></Verse>
    <Verse VerseID="7"><![CDATA[The path of those upon whom Thou has bestowed favours, Not those upon whom wrath is brought down, nor those who go astray.]]></Verse>
</Chapter>

what I want is to read a specific Verse Cdata section. For example if I pass Chapter id and Verse id to a function so it must return the specific cdata content

i tried this block of code

 public  string  getTranslation(int p1, int p2)
    {
        string translationPath = Path.Combine(Package.Current.InstalledLocation.Path, "Data/eglish-translation.xml");
        XDocument document = XDocument.Load(translationPath);
       var  value = (from r in document.Descendants("Chapter").Where
                                  (r => ((int)r.Attribute("ChapterID") == p1)&&(int)r.Attribute("VerseId") == p2))
                 select r.Element("Verse").Value).FirstOrDefault();


          return value;  
    }

But its returning null what i am doing wrong here ?

VerseId is attribute of Verse element, not of Chapter element. Here is correct query:

var value = (from c in document.Descendants("Chapter")
             where (int)c.Attribute("ChapterID") == p1 
             from v in c.Elements("Verse")
             where (int)v.Attribute("VerseID") == p2
             select (string)v).FirstOrDefault();

Or with lambda syntax:

var value = document.Descendants("Chapter")
                    .Where(c => (int)c.Attribute("ChapterID") == p1)
                    .SelectMany(c => c.Elements("Verse"))
                    .Where(v => (int)v.Attribute("VerseID") == p2)
                    .Select(v => (string)v)
                    .FirstOrDefault();

Side note - if HolyQuran is root element of your xml file, then it's better to query for chapters as document.Root.Elements() or document.Root.Elements("Chapter") to avoid traversing whole tree for descendants.

You also can use xpath:

var xpath = 
   String.Format("//Chapter[@ChapterID='{0}']/Verse[@VerseID='{1}']", p1, p2);
var value = (string)document.XPathSelectElement(xpath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM