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

时间:2013-11-12 17:52:32

标签: c# xml

我正在开发一个项目,我不确定如何从XML文件中获取所需的数据。 这是我获取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);
}

这是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>

我遇到的问题是,我使用我编写的代码返回整个类别的标签之间的所有文本。我需要获取每个标记的文本,第一个,第二个,第三个等,以及从XML标记内部获取点值和答案属性值以在我的代码中使用。我不确定我需要做些什么来获得这些值。提前感谢任何想帮助我的人。

3 个答案:

答案 0 :(得分: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

        }

答案 1 :(得分:0)

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

答案 2 :(得分: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;
    }
}

我建议你像这个样本一样编写一个返回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;
}

以下是调用上述方法的代码,通过此方法,您将获得所需的范围

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