我使用以下代码检索并显示XML文件中的所有值。
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
StorageFile xmlFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Content1.xml");
XmlDocument xmlDoc;
xmlDoc = await XmlDocument.LoadFromFileAsync(xmlFile);
System.Xml.Linq.XDocument duc = System.Xml.Linq.XDocument.Parse(xmlDoc.GetXml());
//var query = from Date in xmlDoc.Root.Elements("Serial")
var query = from Date in duc.Root.Elements("Serial")
where Date.Attribute("No").Value == "1"
from Current in Date.Elements("Current")
select new
{
value = Current.Attribute("EnglishDate").Value,
Arabic = Current.Attribute("ArabicDate").Value,
NarratedBy = Current.Attribute("Narrated").Value,
Content = Current.Attribute("HadithBody").Value,
TransmittedBy = Current.Attribute("RefferedBy").Value,
RefferedVerses = Current.Attribute("RefferedVerses").Value
};
foreach (var Date in query)
{
xmlTextBlock.Text=String.Format("{0}\t{1}",Date.NarratedBy,Date.Value,Date.Arabic,Date.Content,Date.TransmittedBy,Date.RefferedVerses)+Environment+NewLine;
}
}
这是我的XML文件
<?xml version="1.0" encoding="utf-8" ?>
<Hadith>
<Serial No="1">
<Current EnglishDate="22 January 2013"
ArabicDate="10 Rabi-Ul-Awal 1434"
Narrated="Narrated Hasan:"
HadithBody="Knowledge is of two types.Firstly,knowledge perceived by the heart,and that is useful knowledge"
RefferedBy="Transmitted by:"
RefferedVerses="Darimi-Al-Timidhi-Hadith 270">
</Current>
</Serial>
</Hadith>
此代码未显示所有值。如何显示我在XML文件中声明的所有值?
答案 0 :(得分:0)
修改 foreach 循环代码:
xmlTextBlock.Text= String.Empty;
foreach (var Date in query)
{
xmlTextBlock.Text += String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n",
Date.NarratedBy,Date.Value,Date.Arabic,Date.Content,
Date.TransmittedBy,Date.RefferedVerses);
}