簡體   English   中英

如何在xml中的節點內部讀取屬性數據

[英]how to read attribute data inside node in 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用於顯示問題

請幫幫我。

試試看(使用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";
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM