问题
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