简体   繁体   中英

Retrieve Element from xml file

I am using the following code to retrieve and display all value from XML file.

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 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?

Modify your foreach loop code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM