1

I have been trying for the past few hours, with little to no success, to read from a .xml file.

I tried:

XmlReader reader = XmlReader.Create("ChampionList.xml");

        reader.ReadToFollowing("Name");
        reader.MoveToFirstAttribute();
        string nume = reader.Value;
        MessageBox.Show(nume);

My xml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<main>
  <Champion>
    <Name>Aatrox</Name>
    <Counter>Soraka</Counter>
  </Champion>
  <Champion>    
    <Name>Ahri</Name>
    <Counter>Diana</Counter>    
  </Champion>
</main>

I would like to read the name and the counter whenever I press the button. Each time a new one (1st button press - 1st champ and so on).

Can someone help me? Also, a bit of an explanation of the code would be nice, if there are many loops and stuff, I still have much to learn.

1
  • Why use XmlReader? Try LINQ to XML (XDocument). Commented Jun 30, 2013 at 19:43

4 Answers 4

1

You might find it easier to work with a higher-level interface than XmlReader. For example, you could do this in Linq to XML as follows:

// read in the entire document
var document = XDocument.Load("ChampionsList.xml");

// parse out the relevant information
// start with all "Champion" nodes
var champs = documents.Descendants("Champion")
    // for each one, select name as the value of the child element Name node
    // and counter as the value of the child element Counter node
    .Select(e => new { name = e.Element("Name").Value, counter = e.Element("Counter").Value });

// now champs is a list of C# objects with properties name and value

foreach (var champ in champs) {
    // do something with champ (e. g. MessageBox.Show)
}
Sign up to request clarification or add additional context in comments.

3 Comments

change Descendants("Name").Single() to Element("Name") and I'll up-vote, and to the "Counter" as well.
Thanks a lot, it worked :) What's the difference between Descendants().Single() and Element()?
@user2491551: in this case, there is none (although Element() should be slightly faster). In general, Descendants() will include all elements inside the parent (including deeply nested ones), while Element() only looks one level down.
1

To test XML validity, I've found it's pretty easy to set the file's extension to .XML and then drop it onto an Internet Explorer window. Internet Explorer has a pretty good XML viewer built-in, and it will let you know if there are errors.

(EDIT: removed specific suggestion about presented XML being invalid--this appears to have been caused by markup issues.)

3 Comments

Are you sure about your claim?
The XML looked different when I answered. This answer is not as useful now other than the tip of checking validity in Internet Explorer.
I only made sure the root tag showed up... I didn't add or remove any tags... your answer made me start editing the question...
1

Use the ReadElementContentAsString to get the contents of an element

XmlReader reader = XmlReader.Create("ChampionList.xml");

reader.ReadToFollowing("Name"); // read until element named Name
string nume = reader.ReadElementContentAsString(); // read its content
MessageBox.Show(nume);

Comments

0

why don't you read them to list once and when ever button pressed just take out from your list. XmlTextReader reader = new XmlTextReader("yourfile.xml");

            string elementName = "";

            List<string[]> Champion = new List<string[]>();
            string name = "";            

            while (reader.Read())  // go throw the xml file
            {

                if (reader.NodeType == XmlNodeType.Element) //get element from xml file 
                {

                    elementName = reader.Name;
                }
                else
                {

                    if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue)) //fet the value of element
                    {
                        switch (elementName) // switch on element name weather Name or Counter
                        {
                            case "Name":
                                name = reader.Value;
                                break;
                            case "Counter":
                                string[] value = new string[] { name, reader.Value }; //store result to list of array of string
                                Champion.Add(value);
                                break;

                        }
                    }
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.