Importing XML in a string array - Windows Phone 7

微笑、不失礼 提交于 2019-12-11 05:21:53

问题


I'm using this function to get the data of the xml file to a string array

 private void ReadXml(string Dictionary, char letter)
    {
        XDocument xml = XDocument.Load(Dictionary);
        string[] dictionary=new string[2];
        int i=0;
        IEnumerable<XElement> de = from el in xml.Descendants(letter.ToString())
                                   select el;
        foreach (XElement el in de)
        {
            dictionary[i++] = el.Value.ToString();
        }
    }

But the array that is produced has all the contents of the xml tag I specify merged in its first line.

For example the xml file

<?xml version="1.0" encoding="Unicode"?>
<Animals>
  <A>Ant</A>
  <B>
    <word>Boa</word>
    <word>Bear</word>
  </B>
</Animals>

and with xml.Descendants("B") produces dictionary[0]=BoaBear and dictionary[1]=NULL

So how can I make it so every word gets in a separate array cell?

Thank you in adnance.


回答1:


You need to loop through the elements inside the <B> tag by calling Elements().

For example:

foreach(var word in xml.Root.Element("B").Elements())


来源:https://stackoverflow.com/questions/6914726/importing-xml-in-a-string-array-windows-phone-7

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