计算节点匹配列表框项的子节点

时间:2012-05-10 13:09:07

标签: c# xml visual-studio-2010 selectedindexchanged

我在这里遇到选择索引更改的问题。我的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+" ");
                }
            }

1 个答案:

答案 0 :(得分: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();