0

I am working on a project and am unsure about how I need to go about getting the data that I need from the XML file. This is the code that I have for getting the XML file and beginning to iterate through it.

public void Load(string xmlFile)
{
    XDocument doc = XDocument.Load(xmlFile);

    var query = from xElem in doc.Descendants("Jeopardy")
                select new Answer
                {
                    Category = Convert.ToString(xElem.Element("category").Value)

                };

    this.Clear();

    AddRange(query);
}

Here is the first part of the XML file

 <?xml version="1.0" encoding="utf-8"?>
<Jeopardy>
  <category name = 'People in Computing'>
    <first points = '100' answer = 'Alan Turing'>Known as the questioner of the human  mind, this man is known for helping tell humans and computers apart.</first>
    <second points = '200' answer = 'Grace Hopper'>This female pioneer of the COBOL computer programming language was an Admiral in the US Navy.</second>
    <third points = '300' answer = 'Tim Berners-Lee'>Called the father of the world wide web, this man is the director of the W3C.</third>
    <fourth points = '400' answer = 'Lawrence Lessig'>An American academic and political activist who founded the Creative Commons, this man lobbies for reduced legal restrictions on copyrights and trademarks in the technology sector.</fourth>
    <fifth points = '500' answer = 'Ada Lovelace'>This woman, known as the world's first computer programmer was also a Countess.</fifth>
  </category>

What I am having trouble with is that I am returning all of the text in between the tags for the entire category with the code I have written. I need to get the text for each tag, first, second, third, etc. as well as getting the point value and answer attribute values from inside the XML tags to use in my code. I am not sure what I need to do to get these values. Thank you ahead of time to anyone who would like to help me out.

4

3 回答 3

0
XmlNode nl = doc.SelectSingleNode("//category");
foreach (XmlNode xmlNode in nl.ChildNodes)
    Console.WriteLine(string.Format("{0} - {1}",xmlNode.Name,xmlNode.FirstChild.Value));
于 2013-11-12T22:04:59.180 回答
0

我不确定您的答案和类别课程。作为您的 XML,您将重复 category 元素,所以我假设它们是这样的:

public class Category
{
    public Category() { }

    public string name { get; set; }
    public Answer first { get; set; }
    public Answer second { get; set; }
    public Answer third { get; set; }
    public Answer fourth { get; set; }
    public Answer fifth { get; set; }
}

public class Answer
{
    public decimal points { get; set; }
    public string answer { get; set; }
    public string description { get; set; }

    public Answer(decimal points, string answer, string description)
    {
        this.points = points;
        this.answer = answer;
        this.description = description;
    }
}

我建议您编写这样的示例代码,该示例返回类别列表:

public List<Category> GetCategoryList(string xmlFile)
{
    XDocument doc = XDocument.Load(xmlFile);
    List<Category> categories = (from xElem in doc.Descendants("category")
                                    select new Category
                                    {
                                        name = xElem.Attribute("name").Value,
                                        first = new Answer(decimal.Parse(xElem.Element("first").Attribute("points").Value),
                                                            xElem.Element("first").Attribute("answer").Value,
                                                            xElem.Element("first").Value),
                                        second = new Answer(decimal.Parse(xElem.Element("second").Attribute("points").Value),
                                                            xElem.Element("second").Attribute("answer").Value,
                                                            xElem.Element("second").Value),
                                        third = new Answer(decimal.Parse(xElem.Element("third").Attribute("points").Value),
                                                            xElem.Element("third").Attribute("answer").Value,
                                                            xElem.Element("third").Value),
                                        fourth = new Answer(decimal.Parse(xElem.Element("fourth").Attribute("points").Value),
                                                            xElem.Element("fourth").Attribute("answer").Value,
                                                            xElem.Element("fourth").Value),

                                        fifth = new Answer(decimal.Parse(xElem.Element("fifth").Attribute("points").Value),
                                                            xElem.Element("fifth").Attribute("answer").Value,
                                                            xElem.Element("fifth").Value),
                                    }).ToList();
    return categories;
}

这是调用上述方法的代码,通过这个你会得到你想要的范围

List<Category> categories = GetCategoryList(@"XMLFile.xml");
foreach (Category c in categories)
{
    //Do get value from Category object
}
于 2013-11-13T03:10:09.433 回答
0

尝试这个

XDocument doc = XDocument.Load(xmlFile);
foreach (XElement item in doc.Element("Jeopardy").Elements("category"))
        {
            first=item.Element("first").Value);//to get the value of first
           first_points=item.Element("first").Attribute("points").Value);//to get the value of points attribute
           first_answer=item.Element("first").Attribute("answer").Value);//to get the value of answer attribute
//same you can do for other tags and attributes

        }
于 2013-11-12T18:04:56.030 回答