Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one xml file which is stored Particular day, timings of the day, and the file name of particular time. How Can I get only particular day's value (timings and file names) using XML Reader in C#. Please can any one help me?

My XML file is like this


XML
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <MediaPlayer>
- <Body>
  <Name>Mp3 Player</Name>
- <Days>
- <Monday>
  <Day>Monday</Day>
  <FiveToNine>Old Kannada</FiveToNine>
  <NineToLeven>New Kannada</NineToLeven>
  <LevenToThirteen>New Hindi</LevenToThirteen>
  <ThirteenToFourteen>Old Hindi</ThirteenToFourteen>
  <ForteenToFifteen>Old English</ForteenToFifteen>
  <FifteenToSeventeen>New English</FifteenToSeventeen>
  <SeventeenToEighteen>Old Malayalam</SeventeenToEighteen>
  <EighteenToTwenty>New Malayalam</EighteenToTwenty>
  <TwentyToTwentytwo>Old Tamil</TwentyToTwentytwo>
  <TwentytwoToZero>New Tamil</TwentytwoToZero>
  <ZeroToFive>Arabic</ZeroToFive>
  </Monday>
- <Tuesday>
  <Day>Tuesday</Day>
  <FiveToNine>AAA</FiveToNine>
  <NineToLeven>BBB</NineToLeven>
  <LevenToThirteen>CCC</LevenToThirteen>
  <ThirteenToFourteen>DDD</ThirteenToFourteen>
  <ForteenToFifteen>EEE</ForteenToFifteen>
  <FifteenToSeventeen>FFF</FifteenToSeventeen>
  <SeventeenToEighteen>GGG</SeventeenToEighteen>
  <EighteenToTwenty>HHH</EighteenToTwenty>
  <TwentyToTwentytwo>III</TwentyToTwentytwo>
  <TwentytwoToZero>JJJ</TwentytwoToZero>
  <ZeroToFive>KKK</ZeroToFive>
  </Tuesday>
  </Days>
  </Body>
  </MediaPlayer>



and my code is like this

C#
XmlTextReader reader = new XmlTextReader(@"D:\MediaPlayer.xml");
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "FiveToNine":
                            reader.Read();
                            textBox1.Text = reader.Value;
                            break;
                        case "NineToLeven":
                            reader.Read();
                            listBox1.Items.Add(reader.Value);
                            break;
                    }
                }
            }
            reader.Close();
Posted

1 solution

It will be easy to use the XMlDocument and Xpath instead of XMlTextReader.Try the following code


string childNodeName = comboBox1.Text;//Monday,Tuesday....
          string XmlFile = @"D:\MediaPlayer.xml";
          if (string.IsNullOrEmpty(childNodeName))
          {
              return;
          }
          XmlDocument xmlDoc = new XmlDocument();
          if (File.Exists(XmlFile))
          {
              xmlDoc.Load(XmlFile);
              XmlNodeList listRoot = xmlDoc.DocumentElement.SelectNodes("/MediaPlayer/Body/Days/" + childNodeName); //Xpath

              foreach (XmlNode nodesDay in listRoot)
              {
                  if (childNodeName == nodesDay.Name)
                  {
                      XmlNodeList listrootDay = nodesDay.ChildNodes;
                      textBox1.Text = nodesDay.Name;
                      foreach (XmlNode nodeChild in listrootDay)
                      {
                          listBox1.Items.Add(nodeChild.Name + "-" + nodeChild.InnerText);
                      }
                  }
              }
          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900