简体   繁体   English

我如何XML数据并从xml文件中提取所需的信息

[英]How do I XML data and pull needed information from xml file

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. 我正在从事一个项目,不确定如何从XML文件获取所需的数据。 This is the code that I have for getting the XML file and beginning to iterate through it. 这是获取XML文件并开始对其进行迭代所需的代码。

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文件的第一部分

 <?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. 我需要获取每个标签(第一,第二,第三等)的文本,以及从XML标签内部获取点值和答案属性值以在我的代码中使用。 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. 提前感谢任何想帮助我的人。

Try this 尝试这个

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

        }
XmlNode nl = doc.SelectSingleNode("//category");
foreach (XmlNode xmlNode in nl.ChildNodes)
    Console.WriteLine(string.Format("{0} - {1}",xmlNode.Name,xmlNode.FirstChild.Value));

I'm not sure about your Answer and Category class. 我不确定您的Answer和Category类。 As your XML, you will repeat category element, so I assume that they are like this: 作为您的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;
    }
}

And I would suggest you to code like this sample which return a list of Category: 我建议您像这样的示例代码那样返回类别列表:

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

Here is a code to call the above method, by this you will get a range you want 这是调用上述方法的代码,这样您将获得所需的范围

List<Category> categories = GetCategoryList(@"XMLFile.xml");
foreach (Category c in categories)
{
    //Do get value from Category object
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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