我有一个充满此类事件的xml文件,需要检查多个语句。怎么称呼这个动作?我是如此迷茫,以至于我什至不知道从哪里开始寻找。
<BENEvents>
<BENDescisionQ1 flagBEN_00="true" flagBEN_01="true" flagBEN_28="true" flagBEN_29="false">
<AskQuestions caption='Looking today again at you glass with milk you decide....'>
<Question event='DescisionQ1Yes'>Cowcostumes look great.</Question>
<Question event='DescisionQ1No'>Flatnesss is justice, so NO</Question>
</AskQuestions>
</BENDescisionQ1>
<BENDescisionQ2 ....>
.....
</BENDescisionQ2>
<BENEvents>
[请帮助为我指明正确的方向。
这不是简单的Xml Parse。使用Regex查看我的Xml Linq解决方案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
BenEvent benEvent = new BenEvent();
benEvent.parse(FILENAME);
}
}
public class BenEvent
{
public static List<BenEvent> listEvents = new List<BenEvent>();
public int number { get; set; }
public List<KeyValuePair<int, Boolean>> flags { get; set; }
public string question { get; set; }
public List<KeyValuePair<string, string>> answers { get; set; }
string patternNumber = @"\d+$";
string patternYesNo = @"[^\d]+$";
public void parse(string filename)
{
XDocument doc = XDocument.Load(filename);
XElement benEvents = doc.Descendants("BENEvents").FirstOrDefault();
foreach (XElement descision in benEvents.Elements())
{
BenEvent newEvent = new BenEvent();
listEvents.Add(newEvent);
newEvent.number = int.Parse(Regex.Match(descision.Name.LocalName, patternNumber).Value);
newEvent.flags = descision.Attributes()
.Select(x => new KeyValuePair<int, Boolean>(int.Parse(Regex.Match(x.Name.LocalName, patternNumber).Value), (Boolean)x)).ToList();
newEvent.question = (string)descision.Element("AskQuestions").Attribute("caption");
newEvent.answers = descision.Descendants("Question")
.Select(x => new KeyValuePair<string, string>(Regex.Match((string)x.Attribute("event"), patternYesNo).Value, (string)x)).ToList();
}
}
}
}