解析XML元素一次

本文关键字:一次 XML 元素 解析 | 更新日期: 2023-09-27 18:19:22

我有一个和平的xml,我需要收集信息。

<response>
<students>
    <student>
        <educationalinstitutionname>Test One</educationalinstitutionname>
        <academicqualificationtype>TestLevel One</academicqualificationtype>
        <starttime>22/02/06</starttime>
        <endtime>19/08/10</endtime>
        <grade>A</grade>
    </student>
    <student>
        <educationalinstitutionname>Test One</educationalinstitutionname>
        <academicqualificationtype>TestLevel Two</academicqualificationtype>
        <starttime>22/02/06</starttime>
        <endtime>19/08/10</endtime>
        <grade>B</grade>
    </student>
    <student>
        <educationalinstitutionname>Test Two</educationalinstitutionname>
        <academicqualificationtype>TestLevel Two</academicqualificationtype>
        <starttime>22/02/06</starttime>
        <endtime>19/08/10</endtime>
        <grade>C</grade>
    </student>
    <student>
        <educationalinstitutionname>Test Two</educationalinstitutionname>
        <academicqualificationtype>TestLevel Three</academicqualificationtype>
        <starttime>22/02/06</starttime>
        <endtime>19/08/10</endtime>
        <grade>D</grade>
    </student>
</students>

我遇到的问题是,我需要解析一次教育机构名称,如果它是相同的,但收集学术资格类型,开始时间,结束时间和成绩,它出现在相同的教育机构名称节点下的一切。

XmlNodeList WordXMLNodeLists = XmlResponceDoc.SelectNodes("/response/students/student");
string GetDetaisl = string.Empty;
foreach (XmlNode GradeItem in WordXMLNodeLists)
{
    string GetStartDate = EducationElement.SelectNodes("/response/students/student/starttime")[0].InnerText;
    string GetEndDate = EducationElement.SelectNodes("/response/students/student/endtime")[0].InnerText;
    string GetEstablishmentName = EducationElement.GetElementsByTagName("educationalinstitutionname")[0].InnerText;
    string GetGrade = EducationElement.SelectNodes("/response/students/student/grade")[0].InnerText;
    GetDetaisl = string.Concat(GetStartDate, " ", GetEndDate, " ", GetEstablishmentName);
    if (GetDetaisl.Length == GetEstablishmentName.Length)
    {
        string GetGrades = GetGrade;
        GetEducationNodeDate.Add(GetGrades);
    }
    else
    {
        GetEducationNodeDate.Add(GetDetaisl);
        continue;
    }
}

我发现的问题是,我似乎无法循环一次名称并收集该名称下的文档中的所有信息。谢谢你提供的任何帮助

解析XML元素一次

你不是很清楚你想要达到什么目标。我假设基本上你想要<student>标签内大多数节点的值:

XmlNodeList WordXMLNodeLists = XmlResponceDoc.SelectNodes("/response/students/student");
foreach (XmlNode GradeItem in WordXMLNodeLists)
{
    string GetStartDate = GradeItem.SelectSingleNode("./starttime").InnerText;
    string GetEndDate = GradeItem.SelectSingleNode("./endtime").InnerText;
    string GetEstablishmentName = GradeItem.SelectSingleNode("./educationalinstitutionname").InnerText;
    string GetGrade = GradeItem.SelectSingleNode("./grade").InnerText;
    //do something with data gathered above
}