1

i want to get the child nodes of suras in the following xml data;

<suras alias="chapters">
    <sura index="1" ayas="7" start="0" name="الفاتحة" tname="Al-Faatiha" ename="The Opening" type="Meccan" order="5" rukus="1" />
    <sura index="2" ayas="286" start="7" name="البقرة" tname="Al-Baqara" ename="The Cow" type="Medinan" order="87" rukus="40" />
    <sura index="3" ayas="200" start="293" name="آل عمران" tname="Aal-i-Imraan" ename="The Family of Imraan" type="Medinan" order="89" rukus="20" />
    <sura index="4" ayas="176" start="493" name="النساء" tname="An-Nisaa" ename="The Women" type="Medinan" order="92" rukus="24" />
    <sura index="5" ayas="120" start="669" name="المائدة" tname="Al-Maaida" ename="The Table" type="Medinan" order="112" rukus="16" />
</suras>

The suras node is comming fine but when i try to access the sura node, i can't get it as an Elements of suras;
enter image description here as you can see if i debug the parent i.e suras node, i can clearly see the first and last node correctly. But when i try to query its Elements or Decendents, i dont' get anything. Check the following screen shot; enter image description here Similarly, if i change my code to Element("sura") instead of Elements("sura"), then it only shows first node check this; enter image description here So, my question is, Why i am not getting the child nodes i.e surah?

Idrees Khan
  • 7,702
  • 18
  • 63
  • 111
  • It's *really* hard to see what's going on with just a debugger screen capture... and "I can't get it" doesn't really explain it well either. Are you seeing problems outside the debugger? It's entirely possible this is just a debugger visualization issue - but if it's not, please produce a short but complete program demonstrating the problem, with console output. – Jon Skeet Aug 02 '14 at 21:04

2 Answers2

2

It is actually worked, you just can't see it that way. Try to iterate through Elements("suras") and you'll get the result just fine :

....
var test2 = test.Elements("sura");
foreach (XElement sura in test2)
{
    MessageBox.Show(sura.ToString())
    //or print to VS output window :
    //Debug.WriteLine(sura.ToString());
}

Or materialize the query to be able to see the actual result without looping, by calling .ToList() or .ToArray() :

var test2 = test.Elements("sura").ToList();
har07
  • 88,338
  • 12
  • 84
  • 137
  • yes i couldn't get the result using visual studio debugging experience, i actually should have looped through it. Thanks (y) – Idrees Khan Aug 03 '14 at 15:30
1

It is because you are not iterating through the Child Nodes.

Refer the below codes:

            var myDoc = new XmlDocument();
            myDoc.Load(Server.MapPath("~/XMLFile1.xml"));

            // Get All Suras Elements
            var AllSuras = myDoc.GetElementsByTagName("suras");

            // For now Select the First item in the All Suras
            var Sura = AllSuras[0].ChildNodes;

            foreach (XmlNode surah in Sura)
            {
                var index = surah.Attributes["index"];
                var ayas = surah.Attributes["ayas"];
                var start = surah.Attributes["start"];
                var name = surah.Attributes["name"];
                var tname = surah.Attributes["tname"];
                var ename = surah.Attributes["ename"];
                var type = surah.Attributes["type"];
                var order = surah.Attributes["order"];
                var rukus = surah.Attributes["rukus"];
            }

Hope this helps!

c0deslayer
  • 515
  • 5
  • 7