C# XML Node Works with Element But Not with Elelments

喜你入骨 提交于 2019-12-11 04:49:13

问题


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;

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;

Similarly, if i change my code to Element("sura") instead of Elements("sura"), then it only shows first node check this;

So, my question is, Why i am not getting the child nodes i.e surah?

回答1:


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();



回答2:


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!



来源:https://stackoverflow.com/questions/25099322/c-sharp-xml-node-works-with-element-but-not-with-elelments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!