2012-05-29 41 views
1

即時通訊在該im中創建一個web應用程序匹配用戶答案與我的xml答案我已經完成所有代碼和我的代碼工作正常然後我已經改變了我的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"); 

上面的線,你可以看到,我已閱讀問題節點,但問題的節點內部有類似的屬性在目前我的問題,你可以在我的XML文件中看到文本。

if (nodexm.InnerText.Trim() == label2.Text) 
在上面的線I M匹配我的xml文件的問題,屏幕顯示問題

,但我不能這樣做that.label2用於顯示的問題

請幫助我。

+0

內部label2.Text請告訴我? – Habib

+0

重複的http://stackoverflow.com/questions/1600065/how-to-read-attribute-value-from-xmlnode-in-c –

+0

你需要訪問文本屬性,「文本」不是一個特殊的標識符在XML中。 if(nodexm.GetAttribute(「Text」)。Trim()== label2.Text) – joocer

回答

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"; 
       } 
      } 
     } 
相關問題