2012-05-10 66 views
1

我在這裏選擇索引更改時遇到問題。我的xml文件包含模塊編號,模塊名稱,評估,學分等等。我試圖在這裏實現的是 - 一些課程細節從xml(模塊名稱和代碼)加載到列表框中,但是當用戶選擇模塊時,標籤應該顯示多少該模塊的評估。 這裏是我的XML文件樣本計算節點匹配列表框項目的子節點項目

<module> 
    <moduleCode>ECWM618</moduleCode> 
    <moduleTitle>Semantic and Social Web</moduleTitle> 
    <credits>15</credits> 
    <semester>2</semester> 
    <assessmentDetails> 
     <assessment> 
     <assessmentName>Coursework1</assessmentName> 
     <assessmentType>Coursework</assessmentType> 
     <assessmentWeighting>25</assessmentWeighting> 
     </assessment> 
     <assessment> 
     <assessmentName>Coursework2</assessmentName> 
     <assessmentType>Coursework</assessmentType> 
     <assessmentWeighting>25</assessmentWeighting> 
     </assessment> 
     <assessment> 
     <assessmentName>Exam</assessmentName> 
     <assessmentType>Exam</assessmentType> 
     <assessmentWeighting>50</assessmentWeighting> 
     </assessment> 
    </assessmentDetails> 
    </module> 

這裏是我得到的代碼

private void moduleSummaryBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      // when module from modulelist is selected, it read all indicies assigned to the module. 
      //!!!!!!!! it reads last node only :(? wtf im tired ...lol 
      //    
       // read data from modulelist 
       string path = Directory.GetCurrentDirectory(); 
       FileStream fs = new FileStream(@"myCourse.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); 
       XmlReader moduleReader = XmlReader.Create(fs); 
       moduleReader.Read(); 
       while (moduleReader.Read()) 
       { 
        int assessmentNo = 0; 
        bool match = false; 
        moduleReader.MoveToElement(); 

        if (moduleReader.Name == "assessmentName") 
        { 
         moduleReader.Read();// reads xml file. 
         XmlNodeType nType = moduleReader.NodeType; //XmlNodeType Specifies the type of node. If node matches specified name = true. 
         if (nType == XmlNodeType.Text)// if node type match XmlNodeType (and there is some text in it) statement becomes true 
         { 
          this.assno.Text = (moduleReader.Value.ToString()); //set mcode label to matched node.        
         } 
        } 

        if (moduleReader.Value.ToString() == moduleSummaryBox.Items[moduleSummaryBox.SelectedIndex].ToString()) 
        { 
         match = true; 
        } 

        if (match == true) 
        { 
         break;       
        } 


      } 
     } 

幫助表示讚賞謝謝:)

編輯: 這是進入列表框

moduleSummaryBox.Items.Clear(); 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(@"myCourse.xml"); 

      XmlNodeList levelList = doc.GetElementsByTagName("level"+l_level); 
      foreach (XmlNode node in levelList) 
      { 
       XmlElement moduleElement = (XmlElement)node; 
       XmlNodeList modules_individ = moduleElement.GetElementsByTagName("module"); 
       foreach (XmlNode nodes in modules_individ) 
       { 
        XmlElement moduleSeperator = (XmlElement)nodes; 
        string ll_moduleCode = moduleSeperator.GetElementsByTagName("moduleCode")[0].InnerText; 
        string ll_moduleTitle = moduleSeperator.GetElementsByTagName("moduleTitle")[0].InnerText;      
        moduleSummaryBox.Items.Add(ll_moduleCode+" : " + ll_moduleTitle+" "); 
       } 
      } 
+0

列表框的確切內容是什麼?向我們展示它包含的字符串。你還應該描述問題的一部分,而不是將它留在代碼的註釋中。在任何情況下,您都不會計算分配節點的數量。 – tinman

+0

爲什麼不使用XPath? http://support.microsoft.com/kb/308333/en-us – 2GDev

+0

謝謝您的回覆。好吧,我有mannaged計數評估數量...但是代碼計數XML內的所有評估,但不是所選索引的數量評估。 –

回答

0

你可以試試用下面的代碼示例: -

const string xmlString = 
       "<module><moduleCode>ECWM618</moduleCode><moduleTitle>Semantic and Social Web</moduleTitle><credits>15</credits>" + 
       "<semester>2</semester><assessmentDetails><assessment><assessmentName>Coursework1</assessmentName><assessmentType>Coursework</assessmentType>" + 
       "<assessmentWeighting>25</assessmentWeighting></assessment><assessment><assessmentName>Coursework2</assessmentName><assessmentType>Coursework</assessmentType>" + 
       "<assessmentWeighting>25</assessmentWeighting></assessment><assessment><assessmentName>Exam</assessmentName><assessmentType>Exam</assessmentType><assessmentWeighting>50</assessmentWeighting></assessment></assessmentDetails></module>"; 

      var xml = XElement.Parse(xmlString); 
      var qry = 
       xml.Descendants() 
        .Where(e => e.Name == "moduleCode" && e.Value == "ECWM618") 
        .Ancestors() 
        .Descendants() 
        .Where(e => e.Name == "assessmentDetails") 
        .Elements("assessment").Count();