[英]Retrieve Element from xml file
I am using the following code to retrieve and display all value from XML file. 我正在使用以下代码来检索和显示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;
}
}
This is my XML file 这是我的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>
This code is not displaying all the values. 此代码未显示所有值。 What do I have to do to display all the values which I declared in the XMLfile?
要显示我在XMLfile中声明的所有值,我该怎么做?
Modify your foreach loop code: 修改您的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.