1

ユーザーの回答とxmlの回答を一致させるWebアプリケーションを作成しているimすべてのコードを実行し、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");

上記の行では、質問ノードを読んだことがわかりますが、質問ノード内には、テキストのような属性があり、私の質問はxmlファイルで確認できます。

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

上記の行では、画面表示の質問とxmlファイルの質問が一致していますが、それはできません。label2は質問の表示に使用されます

お願い助けて。

4

1 に答える 1

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";
                }
            }
        }
于 2012-05-29T08:17:56.500 に答える