XML 反序列化错误 - CLR/System.InvalidOperationException

问题描述 投票:0回答:1

我尝试将 XML 文件反序列化为对象,但我不断收到相同的错误(如下所述)。我相信我已经正确映射了反对意见。我一直在使用这个指南和这个视频作为参考。我认为我在使用 XMLNS 时遇到了问题。另外,我该如何验证 XML 架构?

项目等级:

using System;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using System.Xml;
using System.Xml.Serialization;

    [Serializable]
    [XmlRoot("HSTrn:HighSchoolTranscript", Namespace="urn:org:pesc:message:HighSchoolTranscript:v1.5.0",
   IsNullable = false)]
    public class HighSchoolTranscript
    {   
        public TransmissionData transmissionData { get; set; }
        public Student student{ get; set; }
    }

    public class TransmissionData
    {
        [XmlElement]
        public string DocumentID { get; set; }
        [XmlElement]
        public DateTime CreatedDateTime { get; set; }
        [XmlElement]
        public string DocumentTypeCode { get; set; }
        [XmlElement]
        public string TransmissionType { get; set; }
        [XmlElement]
        public Source source { get; set; }
        [XmlElement]
        public Destination destination { get; set; }
        [XmlElement]
        public string RequestTrackingID { get; set; }
    }

    public class Source
    {
        [XmlElement]
        public Organization organization { get; set; }
    }

    public class Contacts
    {
        [XmlElement]
        public string EmailAddress { get; set; }
    }

    public class Destination
    {
        [XmlElement]
        public Organization organization { get; set; }
    }

    public class Organization
    {
        [XmlElement]
        public string PSIS { get; set; }
        [XmlElement]
        public string LocalOrganizationID { get; set; }
        [XmlElement]
        public string OrganizationName { get; set; }
        [XmlElement]
        public Contacts contacts { get; set; }
        [XmlElement]
        public string Accreditation { get; set; }
    }

    public class Student
    {
        [XmlElement]
        public Person person { get; set; }
        [XmlElement]
        public AcademicRecord academicRecord { get; set; }
        [XmlElement]
        public string Health { get; set; }
        [XmlElement]
        public string NoteMessage { get; set; }
    }

    public class Person
    {
        [XmlElement]
        public int SchoolAssignedPersonID { get; set; }
        [XmlElement]
        public AgencyIdentifier agencyIdentifier { get; set; }
        [XmlElement]
        public int RecipientAssignedID { get; set; }
        [XmlElement]
        public Birth birth { get; set; }
        [XmlElement]
        public Name name { get; set; }
        [XmlElement]
        public string ParentGuardianName { get; set; }
        [XmlElement]
        public ParentGuardian parentGuardian { get; set; }
        [XmlElement]
        public Gender gender { get; set; }
        [XmlElement]
        public Deceased deceased { get; set; }

    }

    public class AgencyIdentifier
    {
        [XmlElement]
        public int AgencyAssignedID { get; set; }
        [XmlElement]
        public string AgencyCode { get; set; }
        [XmlElement]
        public string StateProvinceCode { get; set; }
    }

    public class Birth
    {
        [XmlElement]
        public DateOnly BirthDate { get; set; } //YYYY-MM-DD
    }

    public class Name
    {
        [XmlElement]
        public string FirstName { get; set; }
        [XmlElement]
        public string MiddleName { get; set; }
        [XmlElement]
        public string LastName { get; set; }
    }

    public class ParentGuardian
    {
        public string Name { get; set; }
    }

    public class Gender
    {
        [XmlElement]
        public string GenderCode { get; set; }
    }

    public class Deceased
    {
        public Boolean DeceasedIndicator { get; set; }
    }

    public class AcademicRecord
    {
        [XmlElement]
        public School school { get; set; }
        [XmlElement]
        public string StudentLevel { get; set; }
        [XmlElement]
        public AcademicAward academicaward { get; set; }
        [XmlElement]
        public AcademicSession[] academicsession { get; set; }
        [XmlElement]
        public String NoteMessage { get; set; }
    }

    public class School
    {
        [XmlElement]
        public string OrganizationName { get; set; }
        [XmlElement]
        public LocalOrganizationID localOrganizationID { get; set; }
        [XmlElement]
        public SchoolContacts contacts { get; set; }

    }

    public class LocalOrganizationID
    {
        [XmlElement]
        public string LocalOrganizationIDQualifier { get; set; }
    }

    public class SchoolContacts
    {
        [XmlElement]
        public Address address { get; set; }
        [XmlElement]
        public Phone phone { get; set; }
        [XmlElement]
        public FaxPhone faxphone { get; set; }
    }

    public class Address
    {
        [XmlElement]
        public string AddressLine { get; set; }
        [XmlElement]
        public string City { get; set; }
        [XmlElement]
        public string PostalCode { get; set; }
        [XmlElement]
        public string StateProvince { get; set; }
        [XmlElement]
        public string ItemsElementName { get; set; }
        [XmlElement]
        public DateOnly EffectiveDate { get; set; } //YYYY-MM-DD
        [XmlElement]
        public DateOnly ExpirationDate { get; set; } //YYYY-MM-DD
    }

    public class Phone
    {
        [XmlElement]
        public int PhoneNumber { get; set; }
        [XmlElement]
        public DateOnly EffectiveDate { get; set; } //YYYY-MM-DD
        [XmlElement]
        public DateOnly ExpirationDate { get; set; } //YYYY-MM-DD
    }

    public class FaxPhone
    {
        [XmlElement]
        public int PhoneNumber { get; set; }
        [XmlElement]
        public int EffectiveDate { get; set; }
        [XmlElement]
        public int ExpirationDate { get; set; }
    }

    public class AcademicAward
    {
        [XmlElement]
        public DateOnly AcademicAwardDate { get; set; }
        [XmlElement]
        public Boolean AcademicCompletionIndicator { get; set; }
        [XmlElement]
        public DateOnly AcademicCompletionDate { get; set; }
    }

    public class AcademicSession
    {
        [XmlElement]
        public AcademicSessionDetails academicSessionDetails { get; set; }
        [XmlElement]
        public School school { get; set; }
        [XmlElement]
        public string StudentLevel { get; set; }
        [XmlElement]
        public Course[] course { get; set; }
        [XmlElement]
        public AttendanceCount attendancecount { get; set; }
    }

    public class AcademicSessionDetails
    {
        [XmlElement]
        public DateOnly SessionDesignator { get; set; } // YYYY-MM
        [XmlElement]
        public DateOnly SessionSchoolYear { get; set; } //YYYY-YYYY
        [XmlElement]
        public DateOnly SessionBeginDate { get; set; } //YYYY-MM-DD
        [XmlElement]
        public DateOnly SessionEndDate { get; set; } //YYYY-MM-DD
    }

    public class Course
    {
        [XmlElement]
        public string CourseCreditBasis { get; set; }
        [XmlElement]
        public string CourseCreditUnits { get; set; }
        [XmlElement]
        public string CourseCreditLevel { get; set; }
        [XmlElement]
        public int CourseCreditValue { get; set; }
        [XmlElement]
        public float CourseCreditEarned { get; set; }
        [XmlElement]
        public int CourseAcademicGrade { get; set; }
        [XmlElement]
        public string CourseSupplementalAcademicGrade { get; set; }
        [XmlElement]
        public string CourseAcademicGradeStatusCode { get; set; }
        [XmlElement]
        public int CourseQualityPointsEarned { get; set; }
        [XmlElement]
        public string CourseNumber { get; set; }
        [XmlElement]
        public string AgencyCourseID { get; set; }
        [XmlElement]
        public string CourseTitle { get; set; }
        [XmlElement]
        public DateOnly CourseAddDate { get; set; }
        [XmlElement]
        public DateOnly CourseDropDate { get; set; }
        [XmlElement]
        public CourseOverrideSchool courseoverrideschool { get; set; }
        [XmlElement]
        public DateOnly CourseBeginDate { get; set; }
        [XmlElement]
        public DateOnly CourseEndDate { get; set; }
        [XmlElement]
        public LanguageOfInstruction languageofinstruction { get; set; }
        [XmlElement]
        public string NoteMessage { get; set; }
    }

    public class CourseOverrideSchool
    {
        [XmlElement]
        public string LocalOrganizationID { get; set; }
    }

    public class LanguageOfInstruction
    {
        [XmlElement]
        public string LanguageCode { get; set; }
    }

    public class AttendanceCount
    {
        [XmlElement]
        public int DaysPresent { get; set; }
        [XmlElement]
        public int DaysAbsent { get; set; }
    }

主要课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace XML_Code
{

    public class Program
    {
        public static void Main(string[] args)
        {
            DeserializeXmlToObject();

            Console.WriteLine("Process Complete.");
        }

        private static void DeserializeXmlToObject()
        {
            var xmlSerializer = new XmlSerializer(typeof(HighSchoolTranscript));
            using (var reader = new StreamReader(@"/Users/michaelkennedy/TestFiles/HighSchoolTranscript.xml"))
            {
                var member = (HighSchoolTranscript)xmlSerializer.Deserialize(reader);
            }
        }
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<HSTrn:HighSchoolTranscript xmlns:HSTrn="urn:org:pesc:message:HighSchoolTranscript:v1.5.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:org:pesc:message:HighSchoolTranscript:v1.5.0 HighSchoolTranscript_v1.5.0.xsd">
    <TransmissionData>
        <DocumentID>TNSEECD2023092600033</DocumentID>
        <CreatedDateTime>2023-09-26T09:30:00.4670217-03:00</CreatedDateTime>
        <DocumentTypeCode>RequestedRecord</DocumentTypeCode>
        <TransmissionType>Original</TransmissionType>
        <Source>
            <Organization>
                <PSIS>EECD</PSIS>
                <LocalOrganizationID />
                <OrganizationName>EECDOrgName</OrganizationName>
                <Contacts>
                    <Email>
                        <EmailAddress>EECDEmail</EmailAddress>
                    </Email>
                </Contacts>
                <Accreditation />
            </Organization>
        </Source>
        <Destination>
            <Organization>
                <PSIS>12345678</PSIS>
                <LocalOrganizationID />
                <OrganizationName>Gotham Academy</OrganizationName>
                <Contacts>
                    <Email>
                        <EmailAddress>[email protected]</EmailAddress>
                    </Email>
                </Contacts>
                <Accreditation />
            </Organization>
        </Destination>
        <RequestTrackingID>TNS0000000000000000001</RequestTrackingID>
    </TransmissionData>
    <Student>
        <Person>
            <SchoolAssignedPersonID>0123456789</SchoolAssignedPersonID>
            <AgencyIdentifier>
                <AgencyAssignedID>0123456789</AgencyAssignedID>
                <AgencyCode>State</AgencyCode>
                <StateProvinceCode>NY</StateProvinceCode>
            </AgencyIdentifier>
            <RecipientAssignedID>987654321</RecipientAssignedID>
            <Birth>
                <BirthDate>1915-04-07</BirthDate>
            </Birth>
            <Name>
                <FirstName>Bruce</FirstName>
                <MiddleName>Thomas</MiddleName>
                <LastName>Wayne</LastName>
            </Name>
            <ParentGuardianName />
            <ParentGuardian>
                <Name />
            </ParentGuardian>
            <Gender>
                <GenderCode>Male</GenderCode>
            </Gender>
            <Deceased>
                <DeceasedIndicator>false</DeceasedIndicator>
            </Deceased>
        </Person>
        <AcademicRecord>
            <School>
                <OrganizationName>Benedict Arnold High School</OrganizationName>
                <LocalOrganizationID>
                    <LocalOrganizationIDQualifier>NY</LocalOrganizationIDQualifier>
                </LocalOrganizationID>
                <Contacts>
                    <Address>
                        <AddressLine>1234 Traitor St</AddressLine>
                        <City>New York</City>
                        <PostalCode>A1B 2C3</PostalCode>
                        <StateProvince>NY</StateProvince>
                        <ItemsElementName>:PostalCode</ItemsElementName>
                        <ItemsElementName>:StateProvince</ItemsElementName>
                        <EffectiveDate>0001-01-01</EffectiveDate>
                        <ExpirationDate>0001-01-01</ExpirationDate>
                    </Address>
                    <Phone>
                        <PhoneNumber>123-456-7890</PhoneNumber>
                        <EffectiveDate>0001-01-01</EffectiveDate>
                        <ExpirationDate>0001-01-01</ExpirationDate>
                    </Phone>
                    <FaxPhone>
                        <PhoneNumber>123-456-7890</PhoneNumber>
                        <EffectiveDate>0001-01-01</EffectiveDate>
                        <ExpirationDate>0001-01-01</ExpirationDate>
                    </FaxPhone>
                </Contacts>
            </School>
            <StudentLevel />
            <AcademicAward>
                <AcademicAwardDate>0001-01-01</AcademicAwardDate>
                <AcademicCompletionIndicator>false</AcademicCompletionIndicator>
                <AcademicCompletionDate>0001-01-01</AcademicCompletionDate>
            </AcademicAward>
            <AcademicSession>
                <AcademicSessionDetail>
                    <SessionDesignator>2023-09</SessionDesignator>
                    <SessionName>Semester 1</SessionName>
                    <SessionSchoolYear>2023-2024</SessionSchoolYear>
                    <SessionBeginDate>0001-01-01</SessionBeginDate>
                    <SessionEndDate>0001-01-01</SessionEndDate>
                </AcademicSessionDetail>
                <School>
                    <LocalOrganizationID />
                </School>
                <StudentLevel />
                <Course>
                    <CourseCreditBasis>Regular</CourseCreditBasis>
                    <CourseCreditUnits>Other</CourseCreditUnits>
                    <CourseCreditLevel>TwelfthGrade</CourseCreditLevel>
                    <CourseCreditValue>0</CourseCreditValue>
                    <CourseCreditEarned>0</CourseCreditEarned>
                    <CourseAcademicGrade>IP</CourseAcademicGrade>
                    <CourseSupplementalAcademicGrade />
                    <CourseAcademicGradeStatusCode>InProgress</CourseAcademicGradeStatusCode>
                    <CourseQualityPointsEarned>0</CourseQualityPointsEarned>
                    <CourseNumber>VISART12</CourseNumber>
                    <AgencyCourseID>VAR4AS</AgencyCourseID>
                    <CourseTitle>VISUAL ARTS 12</CourseTitle>
                    <CourseAddDate>0001-01-01</CourseAddDate>
                    <CourseDropDate>0001-01-01</CourseDropDate>
                    <CourseOverrideSchool>
                        <LocalOrganizationID />
                    </CourseOverrideSchool>
                    <CourseBeginDate>0001-01-01</CourseBeginDate>
                    <CourseEndDate>0001-01-01</CourseEndDate>
                    <LanguageOfInstruction>
                        <LanguageCode>ENG</LanguageCode>
                    </LanguageOfInstruction>
                    <NoteMessage>ACAD;PSP</NoteMessage>
                </Course>
                <Course>
                    <CourseCreditBasis>Regular</CourseCreditBasis>
                    <CourseCreditUnits>Other</CourseCreditUnits>
                    <CourseCreditLevel>TwelfthGrade</CourseCreditLevel>
                    <CourseCreditValue>0</CourseCreditValue>
                    <CourseCreditEarned>0</CourseCreditEarned>
                    <CourseAcademicGrade>IP</CourseAcademicGrade>
                    <CourseSupplementalAcademicGrade />
                    <CourseAcademicGradeStatusCode>InProgress</CourseAcademicGradeStatusCode>
                    <CourseQualityPointsEarned>0</CourseQualityPointsEarned>
                    <CourseNumber>PHL12</CourseNumber>
                    <AgencyCourseID>PSE4AS</AgencyCourseID>
                    <CourseTitle>PHYS ED LEADERSHIP 12</CourseTitle>
                    <CourseAddDate>0001-01-01</CourseAddDate>
                    <CourseDropDate>0001-01-01</CourseDropDate>
                    <CourseOverrideSchool>
                        <LocalOrganizationID />
                    </CourseOverrideSchool>
                    <CourseBeginDate>0001-01-01</CourseBeginDate>
                    <CourseEndDate>0001-01-01</CourseEndDate>
                    <LanguageOfInstruction>
                        <LanguageCode>ENG</LanguageCode>
                    </LanguageOfInstruction>
                    <NoteMessage>ACAD;PSP</NoteMessage>
                </Course>
                <Course>
                    <CourseCreditBasis>Regular</CourseCreditBasis>
                    <CourseCreditUnits>Other</CourseCreditUnits>
                    <CourseCreditLevel>TwelfthGrade</CourseCreditLevel>
                    <CourseCreditValue>0</CourseCreditValue>
                    <CourseCreditEarned>0</CourseCreditEarned>
                    <CourseAcademicGrade>IP</CourseAcademicGrade>
                    <CourseSupplementalAcademicGrade />
                    <CourseAcademicGradeStatusCode>InProgress</CourseAcademicGradeStatusCode>
                    <CourseQualityPointsEarned>0</CourseQualityPointsEarned>
                    <CourseNumber>IBLAL12HL</CourseNumber>
                    <AgencyCourseID>ENG4VB</AgencyCourseID>
                    <CourseTitle>IB ENGLISH A LANGUAGE &amp;amp; LITERATURE HL 12</CourseTitle>
                    <CourseAddDate>0001-01-01</CourseAddDate>
                    <CourseDropDate>0001-01-01</CourseDropDate>
                    <CourseOverrideSchool>
                        <LocalOrganizationID />
                    </CourseOverrideSchool>
                    <CourseBeginDate>0001-01-01</CourseBeginDate>
                    <CourseEndDate>0001-01-01</CourseEndDate>
                    <LanguageOfInstruction>
                        <LanguageCode>ENG</LanguageCode>
                    </LanguageOfInstruction>
                    <NoteMessage>ADV;PSP</NoteMessage>
                </Course>
                <AttendanceCount>
                    <DaysPresent>0</DaysPresent>
                    <DaysAbsent>0</DaysAbsent>
                </AttendanceCount>
            </AcademicSession>
            <AcademicSession>
                <AcademicSessionDetail>
                    <SessionDesignator>2024-02</SessionDesignator>
                    <SessionName>Semester 2</SessionName>
                    <SessionSchoolYear>2023-2024</SessionSchoolYear>
                    <SessionBeginDate>0001-01-01</SessionBeginDate>
                    <SessionEndDate>0001-01-01</SessionEndDate>
                </AcademicSessionDetail>
                <School>
                    <LocalOrganizationID />
                </School>
                <StudentLevel />
                <Course>
                    <CourseCreditBasis>Regular</CourseCreditBasis>
                    <CourseCreditUnits>Other</CourseCreditUnits>
                    <CourseCreditLevel>TwelfthGrade</CourseCreditLevel>
                    <CourseCreditValue>0</CourseCreditValue>
                    <CourseCreditEarned>0</CourseCreditEarned>
                    <CourseAcademicGrade>IP</CourseAcademicGrade>
                    <CourseSupplementalAcademicGrade />
                    <CourseAcademicGradeStatusCode>InProgress</CourseAcademicGradeStatusCode>
                    <CourseQualityPointsEarned>0</CourseQualityPointsEarned>
                    <CourseNumber>GGS12</CourseNumber>
                    <AgencyCourseID>GGY4AS</AgencyCourseID>
                    <CourseTitle>GLOBAL GEOGRAPHY 12</CourseTitle>
                    <CourseAddDate>0001-01-01</CourseAddDate>
                    <CourseDropDate>0001-01-01</CourseDropDate>
                    <CourseOverrideSchool>
                        <LocalOrganizationID />
                    </CourseOverrideSchool>
                    <CourseBeginDate>0001-01-01</CourseBeginDate>
                    <CourseEndDate>0001-01-01</CourseEndDate>
                    <LanguageOfInstruction>
                        <LanguageCode>ENG</LanguageCode>
                    </LanguageOfInstruction>
                    <NoteMessage>ACAD;PSP</NoteMessage>
                </Course>
                <Course>
                    <CourseCreditBasis>Regular</CourseCreditBasis>
                    <CourseCreditUnits>Other</CourseCreditUnits>
                    <CourseCreditLevel>TwelfthGrade</CourseCreditLevel>
                    <CourseCreditValue>0</CourseCreditValue>
                    <CourseCreditEarned>0</CourseCreditEarned>
                    <CourseAcademicGrade>IP</CourseAcademicGrade>
                    <CourseSupplementalAcademicGrade />
                    <CourseAcademicGradeStatusCode>InProgress</CourseAcademicGradeStatusCode>
                    <CourseQualityPointsEarned>0</CourseQualityPointsEarned>
                    <CourseNumber>MT12</CourseNumber>
                    <AgencyCourseID>MTH4AS</AgencyCourseID>
                    <CourseTitle>MATHEMATICS 12</CourseTitle>
                    <CourseAddDate>0001-01-01</CourseAddDate>
                    <CourseDropDate>0001-01-01</CourseDropDate>
                    <CourseOverrideSchool>
                        <LocalOrganizationID />
                    </CourseOverrideSchool>
                    <CourseBeginDate>0001-01-01</CourseBeginDate>
                    <CourseEndDate>0001-01-01</CourseEndDate>
                    <LanguageOfInstruction>
                        <LanguageCode>ENG</LanguageCode>
                    </LanguageOfInstruction>
                    <NoteMessage>ACAD;PSP</NoteMessage>
                </Course>
                <AttendanceCount>
                    <DaysPresent>0</DaysPresent>
                    <DaysAbsent>0</DaysAbsent>
                </AttendanceCount>
            </AcademicSession>
            <NoteMessage>Benedict Arnold High School</NoteMessage>
        </AcademicRecord>
        <Health />
        <NoteMessage>13.00</NoteMessage>
    </Student>
</HSTrn:HighSchoolTranscript>

错误信息:

发生异常:CLR/System.InvalidOperationException System.Private.Xml.dll 中发生类型为“System.InvalidOperationException”的未处理异常:“XML 文档 (2, 2) 中存在错误。” 发现内部异常,请参阅变量窗口中的 $exception 了解更多详细信息。 最里面的异常 System.InvalidOperationException :不是预期的。 在 Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationReaderHighSchoolTranscript.Read31_Item() 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,Void** 参数,签名 sig,布尔 isConstructor) 在 System.Reflection.MethodInvoker.Invoke(对象 obj,IntPtr* args,BindingFlags invokeAttr)

c# xml xml-parsing linq-to-xml
1个回答
0
投票

您未在 [XmlRoot] 中包含前缀,并且名称区分大小写。我更正了下面的根类

   [XmlRoot(ElementName = "HighSchoolTranscript", Namespace = "urn:org:pesc:message:HighSchoolTranscript:v1.5.0", IsNullable = false)]
    public class HighSchoolTranscript
    {
        [XmlElement(ElementName = "TransmissionData", Namespace = "")]
        public TransmissionData transmissionData { get; set; }
        [XmlElement(ElementName = "Student", Namespace = "")]
        public Student student { get; set; }
    }
© www.soinside.com 2019 - 2024. All rights reserved.