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+" ");
                }
            }
4

1 回答 1

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();
于 2012-05-11T07:04:14.500 回答