Question

I have an XML file in the following format:

<Votes>
   <Candidate>
      <CandidateName>Boris Johnson</CandidateName>
   </Candidate>
   <Vote>
      <VoteString>1 3 6 2 9 7 4 5 8</VoteString>
   </Vote>
</Votes>

I want to first read the CandidateName and then the VoteString. I am using the following code:

using (XmlReader reader = XmlReader.Create(filepath))
{
   if (reader.IsStartElement())
   {
      switch (reader.Name)
      {
         case "Candidate":
         string name = reader["CandidateName"];
         break;

         case "Vote":
         string voteStr = reader["VoteString"];
         break;
      }
   }
}

What happens in debug is that reader.Name is set to "Votes" and the two cases in my switch statement never get triggered. I've looked through the XmlReader methods to find something that lists all the elements but there is nothing.

I've not worked directly with XML files before so I'm not sure if the XML format is used is correct. Am I going about this the right way?

Was it helpful?

Solution

XML reader is going to read through each element in the XML tree, so you just need to keep going if you haven't hit the nodes you want yet by putting the reader in a loop. You're code needs to continue to loop through the XML, so it would need to look a little more like this:

using (XmlReader reader = XmlReader.Create(filepath))
{
   while(reader.Read())
   {
      if (reader.IsStartElement())
      {
         switch (reader.Name)
         {
            case "Candidate":
            string name = reader["CandidateName"];
            break;

            case "Vote":
            string voteStr = reader["VoteString"];
            break;
         }
      }
   }
}

Of course, the more important question here is what are you trying to do with those values you are getting? Your current code doesn't actually do anything with the values except assign them to a variable that goes out of scope immediately.

OTHER TIPS

XmlReader is good for fast, forward read, operations. You can accomplish what you're wanting to do by moving the reader forward with Read, Skip, and a whole host of other methods exposed by the reader, in a loop. Or, you can switch to XmlDocument and select the nodes by name, id, xpath, etc.

XmlReader: while(reader.Read()) { ... }

XmlDocument: XmlDocument doc = new XmlDocument(); doc.Load(filepath);

var nodes = doc.GetELementsByTagName("CandidateName"); nodes = doc.GetElementsByTagName("Vote");

I recently wrote and published a library that allows querying and displaying nodes, child nodes, and list nodes within an XML file.

using DxF.XML.Finder;

using (MemoryStream xmlStream = new MemoryStream(File.ReadAllBytes("Sample.xml")))
{
    using (XmlReader xmlReader = new XmlReader(xmlStream))
    {
        // Get the total number of 'book' nodes
        int countBook = xmlReader.GetNodeCount("book");

        for (int i = 0; i < countBook; i++)
        {
            // Retrieve the value of specific nodes for each book
            string authorValue = xmlReader.GetNode("book[" + i + "]", "author");
            string titleValue = xmlReader.GetNode("book[" + i + "]", "title");
            string genreValue = xmlReader.GetNode("book[" + i + "]", "genre");
            string priceValue = xmlReader.GetNode("book[" + i + "]", "price");
            string publish_dateValue = xmlReader.GetNode("book[" + i + "]", "publish_date");
            string descriptionValue = xmlReader.GetNode("book[" + i + "]", "description");

            // Print the book details
            Console.WriteLine("----Book " + i + "--------");
            Console.WriteLine("Author: " + authorValue);
            Console.WriteLine("Title: " + titleValue);
            Console.WriteLine("Genre: " + genreValue);
            Console.WriteLine("Price: " + priceValue);
            Console.WriteLine("Publish Date: " + publish_dateValue);
            Console.WriteLine("Description:");
            Console.WriteLine(descriptionValue);
        }
    }
}

https://github.com/ksomaz/DxF.XML

If you need anything, just let me know :)

Best regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top