C# 如何从XML文件中提取XML数据和所需信息
我正在从事一个项目,不确定如何从XML文件中获取所需的数据。 这是我用来获取XML文件并开始遍历它的代码C# 如何从XML文件中提取XML数据和所需信息,c#,xml,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 {
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>
他被称为人类思维的发问者,以帮助区分人类和计算机而闻名。
这位COBOL计算机编程语言的女性先驱是美国海军上将。
这位被称为万维网之父的人是W3C的主管。
这位美国学术和政治活动家创立了知识共享,他游说减少对技术领域版权和商标的法律限制。
这位被称为世界上第一位计算机程序员的女士也是一位伯爵夫人。
我遇到的问题是,我用我编写的代码返回整个类别标记之间的所有文本。我需要获取每个标记(第一、第二、第三等)的文本,以及从XML标记内部获取要在代码中使用的点值和应答属性值。我不确定我需要做什么才能得到这些值。提前感谢所有愿意帮助我的人。试试这个
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
}
我不确定你的答案和分类。作为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;
}
public List GetCategoryList(字符串xmlFile)
{
XDocument doc=XDocument.Load(xmlFile);
列表类别=(来自文档子体(“类别”)中的xElem)
选择新类别
{
name=xElem.Attribute(“name”).Value,
first=新答案(decimal.Parse(xElem.Element(“first”).Attribute(“points”).Value),
xElem.Element(“第一”).Attribute(“应答”).Value,
元素(“第一”)值),
second=新答案(decimal.Parse(xElem.Element(“second”).Attribute(“points”).Value),
xElem.Element(“第二”).Attribute(“应答”).Value,
元素(“第二”)值),
第三个=新答案(decimal.Parse(xElem.Element(“第三”)属性(“points”).Value),
xElem.Element(“第三”)属性(“答案”)值,
元素(“第三”)值),
第四个=新答案(decimal.Parse(xElem.Element(“第四”)属性(“points”).Value),
元素(“第四”)属性(“答案”)值,
元素(“第四”)值),
第五个=新答案(decimal.Parse(xElem.Element(“第五”)属性(“points”).Value),
元素(“第五”)属性(“答案”)值,
xElem.元素(“第五”)值),
}).ToList();
退货类别;
}
下面是一个调用上述方法的代码,通过它您将获得所需的范围
List<Category> categories = GetCategoryList(@"XMLFile.xml");
foreach (Category c in categories)
{
//Do get value from Category object
}
List categories=GetCategoryList(@“XMLFile.xml”);
foreach(类别中的c类)
{
//是否从类别对象获取值
}
谢谢您的帮助。我喜欢这种设置方式,并感谢反馈。它帮助我在我需要的地方得到了我需要的部件,这样我就可以正确地使用这些信息。我很高兴听到它的帮助^^
List<Category> categories = GetCategoryList(@"XMLFile.xml");
foreach (Category c in categories)
{
//Do get value from Category object
}