Asp.net 如何在c中比较xml节点内的关键字#
我已经创建了一个asp.net页面,该页面正在读取xml文件节点中的一些文本。 下面是我的xml文件的外观Asp.net 如何在c中比较xml节点内的关键字#,asp.net,xml,c#-4.0,Asp.net,Xml,C# 4.0,我已经创建了一个asp.net页面,该页面正在读取xml文件节点中的一些文本。 下面是我的xml文件的外观 <?xml version="1.0" encoding="utf-8" ?> <Questions> <Question id="1">What is IL code </Question> <Answer1>Half compiled,Partially compiled code </Answer1>
<?xml version="1.0" encoding="utf-8" ?>
<Questions>
<Question id="1">What is IL code </Question>
<Answer1>Half compiled,Partially compiled code </Answer1>
<Question id="2">What is TL code </Question>
<Answer2>Half compiled,Partially compiled code </Answer2>
</Questions>
我想显示用户为特定问题输入的答案的百分比
例如,假设问题是……什么是IL代码?用户以部分编译的形式输入答案。因此,我只想检查xml应答节点中输入的kweyword
如果用户答案与节点答案匹配,则以百分比显示答案的准确性
请帮忙
谢谢,如评论中所述,所有答案元素都应该有相同的标签 如果不限制使用,且答案标签的格式为AnswerXXX,则以下代码用于确定问题、答案和答案百分比(总问题/总答案)以及正确答案百分比(正确答案/总答案) 您可以根据自己的具体需要定制比较逻辑
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Questions>" +
"<Question id=\"1\">What is IL code </Question>" +
"<Answer1>Half compiled,Partially compiled code </Answer1>"+
"<Question id=\"2\">What is TL code </Question>"+
"<Answer2>Half compiled,Partially compiled code1 </Answer2>"+
"</Questions>";
var correctAnswerText1 = "Half compiled,Partially compiled code ";// set it to txtUserAnswer.Text
XElement root= XElement.Parse(xml); // Load String to XElement
var questions = root.Elements("Question"); // All questions tag
var answers = root.Elements().Where(e=> e.Name.LocalName.Contains("Answer")); //All answers tag
var correctAnswers = answers.Where( e=> !e.IsEmpty && String.Equals(e.Value, correctAnswerText1)); // All correct answers, here answer comparision logic can be customized
var answerPercent = questions.Count()*100/answers.Count();
var correctAnswerPercent = questions.Count()*100/answers.Count();
questions.Dump();
answers.Dump();
correctAnswers.Dump();
percantage.Dump();
//root.Dump();
var xml=“”+
"" +
“什么是IL代码”+
半编译、半编译代码+
“什么是TL代码”+
半编译、部分编译代码1+
"";
var correctAnswerText1=“半编译、部分编译代码”//将其设置为txtUserAnswer.Text
XElement root=XElement.Parse(xml);//将字符串加载到XElement
var questions=root.Elements(“问题”);//所有问题标签
var answers=root.Elements(),其中(e=>e.Name.LocalName.Contains(“Answer”)//所有答案标签
var correctAnswers=answers.Where(e=>!e.IsEmpty&&String.Equals(e.Value,correctAnswerText1));//所有正确答案,这里的答案比较逻辑可以定制
var answerPercent=questions.Count()*100/answers.Count();
var correctandswerpercent=questions.Count()*100/answers.Count();
问题。转储();
答案:Dump();
正确答案。Dump();
percantage.Dump();
//root.Dump();
您的XML文件必须具有这种格式吗?每一个问题都有一个元素,答案在这个元素中,而不是在问题和答案之间交替,这将更明智。这与问题没有什么特别的关系(XML部分中没有一部分是——你真的只是对字符串相似性感兴趣),但这会让你的生活更轻松。再加上Jon的评论——每个答案都有不同的元素类型似乎特别糟糕(与问题相反,所有问题都使用相同的元素类型)你能给我举个例子让我好好利用吗……谢谢你宝贵的评论
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Questions>" +
"<Question id=\"1\">What is IL code </Question>" +
"<Answer1>Half compiled,Partially compiled code </Answer1>"+
"<Question id=\"2\">What is TL code </Question>"+
"<Answer2>Half compiled,Partially compiled code1 </Answer2>"+
"</Questions>";
var correctAnswerText1 = "Half compiled,Partially compiled code ";// set it to txtUserAnswer.Text
XElement root= XElement.Parse(xml); // Load String to XElement
var questions = root.Elements("Question"); // All questions tag
var answers = root.Elements().Where(e=> e.Name.LocalName.Contains("Answer")); //All answers tag
var correctAnswers = answers.Where( e=> !e.IsEmpty && String.Equals(e.Value, correctAnswerText1)); // All correct answers, here answer comparision logic can be customized
var answerPercent = questions.Count()*100/answers.Count();
var correctAnswerPercent = questions.Count()*100/answers.Count();
questions.Dump();
answers.Dump();
correctAnswers.Dump();
percantage.Dump();
//root.Dump();