C# 如何浏览xml节点?
我在windows窗体和xml文件中有两个按钮和三个标签 这是我的xml:C# 如何浏览xml节点?,c#,xml,C#,Xml,我在windows窗体和xml文件中有两个按钮和三个标签 这是我的xml: <Qustions> <Question id="1" Cat="5"> "Question Text 1" </Question> <Question id="2" Cat="2"> "Question Text 2" </Question> <Question id="3" Cat="3"> "Ques
<Qustions>
<Question id="1" Cat="5">
"Question Text 1"
</Question>
<Question id="2" Cat="2">
"Question Text 2"
</Question>
<Question id="3" Cat="3">
"Question Text 3"
</Question>
<Question id="4" Cat="5">
"Question Text 4"
</Question>
<Question id="5" Cat="8">
"Question Text 5"
</Question>
</Qustions>
“问题案文1”
“问题案文2”
“问题文本3”
“问题案文4”
“问题案文5”
我想显示表单中的每个问题主体和类别,并通过按钮浏览它们
哪种方法是实现这一目标的最佳方法?我建议您试试Linq,
我总是使用linq来浏览xml节点和属性
这是链接我建议你试试Linq,
我总是使用linq来浏览xml节点和属性
下面是链接尝试以下使用XML Linq的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public int questionIndex = 0;
public List<XElement> questions = null;
public Form1()
{
InitializeComponent();
string xml =
"<Qustions>" +
"<Question id=\"1\" Cat=\"5\">" +
"\"Question Text 1\"" +
"</Question>" +
"<Question id=\"2\" Cat=\"2\">" +
"\"Question Text 2\"" +
"</Question>" +
"<Question id=\"3\" Cat=\"3\">" +
"\"Question Text 3\"" +
"</Question>" +
"<Question id=\"4\" Cat=\"5\">" +
"\"Question Text 4\"" +
"</Question>" +
"<Question id=\"5\" Cat=\"8\">" +
"\"Question Text 5\"" +
"</Question>" +
"</Qustions>";
XDocument doc = XDocument.Parse(xml);
questions = doc.Descendants("Question").ToList();
DisplayQuestion(questionIndex);
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
if (questionIndex != 0)
{
DisplayQuestion(--questionIndex);
}
}
private void buttonNext_Click(object sender, EventArgs e)
{
if (questionIndex < questions.Count - 1)
{
DisplayQuestion(++questionIndex);
}
}
public void DisplayQuestion(int index)
{
textBoxId.Text = questions[index].Attribute("id").Value;
textBoxCat.Text = questions[index].Attribute("Cat").Value;
textBoxCatText.Text = questions[index].Value;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共指数=0;
公共列表问题=null;
公共表格1()
{
初始化组件();
字符串xml=
"" +
"" +
“\“问题文本1”+
"" +
"" +
“\“问题文本2”+
"" +
"" +
“\“问题文本3”+
"" +
"" +
“\“问题文本4”+
"" +
"" +
“\“问题文本5”+
"" +
"";
XDocument doc=XDocument.Parse(xml);
questions=doc.substands(“问题”).ToList();
显示问题(问题索引);
}
私有无效按钮前一次单击(对象发送者,事件参数e)
{
如果(问题索引!=0)
{
显示问题(--问题索引);
}
}
私有无效按钮下一步单击(对象发送者,事件参数e)
{
如果(问题索引<问题计数-1)
{
显示问题(++问题索引);
}
}
公共问题(整数索引)
{
Text=questions[index].Attribute(“id”).Value;
Text=questions[index].Attribute(“Cat”).Value;
Text=questions[index].Value;
}
}
}
尝试以下使用XML Linq的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public int questionIndex = 0;
public List<XElement> questions = null;
public Form1()
{
InitializeComponent();
string xml =
"<Qustions>" +
"<Question id=\"1\" Cat=\"5\">" +
"\"Question Text 1\"" +
"</Question>" +
"<Question id=\"2\" Cat=\"2\">" +
"\"Question Text 2\"" +
"</Question>" +
"<Question id=\"3\" Cat=\"3\">" +
"\"Question Text 3\"" +
"</Question>" +
"<Question id=\"4\" Cat=\"5\">" +
"\"Question Text 4\"" +
"</Question>" +
"<Question id=\"5\" Cat=\"8\">" +
"\"Question Text 5\"" +
"</Question>" +
"</Qustions>";
XDocument doc = XDocument.Parse(xml);
questions = doc.Descendants("Question").ToList();
DisplayQuestion(questionIndex);
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
if (questionIndex != 0)
{
DisplayQuestion(--questionIndex);
}
}
private void buttonNext_Click(object sender, EventArgs e)
{
if (questionIndex < questions.Count - 1)
{
DisplayQuestion(++questionIndex);
}
}
public void DisplayQuestion(int index)
{
textBoxId.Text = questions[index].Attribute("id").Value;
textBoxCat.Text = questions[index].Attribute("Cat").Value;
textBoxCatText.Text = questions[index].Value;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共指数=0;
公共列表问题=null;
公共表格1()
{
初始化组件();
字符串xml=
"" +
"" +
“\“问题文本1”+
"" +
"" +
“\“问题文本2”+
"" +
"" +
“\“问题文本3”+
"" +
"" +
“\“问题文本4”+
"" +
"" +
“\“问题文本5”+
"" +
"";
XDocument doc=XDocument.Parse(xml);
questions=doc.substands(“问题”).ToList();
显示问题(问题索引);
}
私有无效按钮前一次单击(对象发送者,事件参数e)
{
如果(问题索引!=0)
{
显示问题(--问题索引);
}
}
私有无效按钮下一步单击(对象发送者,事件参数e)
{
如果(问题索引<问题计数-1)
{
显示问题(++问题索引);
}
}
公共问题(整数索引)
{
Text=questions[index].Attribute(“id”).Value;
Text=questions[index].Attribute(“Cat”).Value;
Text=questions[index].Value;
}
}
}
老实说,这有点宽泛-您需要解析XML,可能需要将其转换为一些问题
对象,然后实现一些东西来浏览它们并将内容绑定到UI您尝试了什么?您有什么特别的问题吗?Xml是保存数据的好方法吗?或者有更好的方法来实现这一点。我还需要记录人员的测试结果。我选择xml是因为问题和人数都在100以下。我需要使用sql db吗?添加一些演示代码
。。我们不会为您编写完整的代码。老实说,这有点宽泛-您需要解析XML,可能需要将其转换为某个问题对象,然后实现一些东西来浏览它们并将内容绑定到UI您尝试了什么?您有什么特别的问题吗?Xml是保存数据的好方法吗?或者有更好的方法来实现这一点。我还需要记录人员的测试结果。我选择xml是因为问题和人数都在100以下。我需要使用sql db吗?添加一些演示代码
。。我们不会为您编写完整的代码。。