如何在xml中读取节点内的属性数据

时间:2012-05-29 07:50:48

标签: c# .net xml

即时创建一个Web应用程序即可将用户答案与我的xml答案相匹配我已完成所有代码,我的代码工作正常,然后我更改了我的xml格式,所以现在我无法读取我的xml文件节点的属性。 以下是我的xml文件。

<?xml version="1.0" encoding="utf-8" ?>
<Exam>
  <Question number="1" Text="What is IL Code">
    <Answer Text="Half compiled, Partially compiled code"> </Answer>
  </Question>
  <Question number="2" Text="What is JIT">
    <Answer Text="IL code to machine language"> </Answer>
  </Question>
  <Question number="3" Text="What is CLR">
    <Answer Text="Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)"> </Answer>
  </Question>
</Exam> 

以下是我的剪辑代码。

XmlDocument docQuestionList = new XmlDocument();// Set up the XmlDocument //
            docQuestionList.Load(@"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml"); //Load the data from the file into the XmlDocument //
            XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");
            foreach (XmlNode nodexm in QuestionList)
            {
                if (**nodexm.InnerText.Trim()** == label2.Text)
                {
                    string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
                    string[] arrXMLAnswer = nodexm.NextSibling.InnerText.Trim().ToLower().Split(' ');
                    List<string> lststr1 = new List<string>();
                    foreach (string nextStr in arrXMLAnswer)
                    {
                        if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
                        {
                            lststr1.Add(nextStr);
                        }
                    }
                    if (lststr1.Count > 0)
                    {
                        label4.Visible = true;
                        label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                    }
                    else
                    {
                        label4.Text = "Your Answer is Wrong";
                    }
                }
            }

XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");

上面一行你可以看到我已经阅读了问题节点,但在问题节点内部有像Text这样的属性,我的问题出现在你的xml文件中。

if (nodexm.InnerText.Trim() == label2.Text)

在上面一行中我将屏幕显示问题与我的xml文件问题相匹配但是我不能这样做.label2用于显示问题

请帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个(使用System.Linq; 使用System.Xml; 使用System.Xml.Linq;)

 XDocument map = XDocument.Parse("<Exam> " +
   "<Question number= \"1\" Text=\"What is IL Code\">" +
    " <Answer Text=\"Half compiled, Partially compiled code\"> </Answer>" +
   "</Question>" +
   "<Question number=\"2\" Text=\"What is JIT\">" +
   "  <Answer Text=\"IL code to machine language\"> </Answer>" +
        "</Question>" +
   "<Question number=\"3\" Text=\"What is CLR\">" +
   "  <Answer Text=\"Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)\"> </Answer>" +
   "</Question>" +
 "</Exam>");
        var atts = map.Descendants("Question").Attributes().Where(a => a.Name == "Text").ToList();
        var QuestionList = map.Descendants("Question").ToList();
        foreach (XElement nodexm in QuestionList)
        {
            if((string)nodexm.Attributes("Text").FirstOrDefault()== label2.Text)
            {
                string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
                string[] arrXMLAnswer = nodexm.Elements("Answer").SelectMany(o => ((string)o.Attribute("Text")).Split(',')).ToArray();
                List<string> lststr1 = new List<string>();
                foreach (string nextStr in arrXMLAnswer)
                {
                    if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
                    {
                        lststr1.Add(nextStr);
                    }
                }
                if (lststr1.Count > 0)
                {
                    label4.Visible = true;
                    label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                }
                else
                {
                    label4.Text = "Your Answer is Wrong";
                }
            }
        }