2
<?xml version="1.0" encoding="utf-8" ?>
<questions>
<question num="1">Employees should be involved in setting their goals.</question>
<question num="2">Most people resists change.</question>
<question num="3">Manager should guide rather control.</question>
<question num="4">Average person is easily decieved.</question>
</questions>
如何將當前屬性值設置爲變量i,當調用next()方法時使用此當前屬性i,它應該檢索下一個節點值(例如,如果當前屬性值爲1時()被稱爲它應該檢索第二個節點的值,當再次調用next()它應該檢索第三個屬性值)使用System.Xml?如何獲取Xml中的當前屬性值並使用當前屬性值檢索下一個節點值?
我試圖使用Xpath檢索第一個節點值,但我不知道如何將當前屬性值設置爲變量,然後使用變量移動到下一個節點值。以下是我嘗試過的代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
namespace ConsoleApplication14
{
interface IQuestion
{
void Question1();
void Next();
}
class Question : IQuestion
{
string BuildXpathQuery(int c)
{
string part1 = @"questions/question[@num='";
string part2 = @"']";
string MyQuery = part1 + c + part2;
return MyQuery;
}
public void Question1()
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\Murari\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication14\\ConsoleApplication14\\XMLFile1.xml");
XmlNode xnList = doc.SelectSingleNode(BuildXpathQuery(1));
if (xnList != null)
{
Console.WriteLine(xnList.InnerXml);
Console.ReadKey();
}
}
public void Next()
{
}
}
class Program
{
static void Main(string[] args)
{
IQuestion ques = new Question();
ques.Question1();
ques.Next();
}
}
}