C# Can';t序列化自定义类型

C# Can';t序列化自定义类型,c#,.net,xml-serialization,xsd,C#,.net,Xml Serialization,Xsd,我正在尝试扩展hr xml类型中的UserArea,ProcessOrganizationChartDataAreaType 我定义了类型OrganizationChartUserArea,它继承自UserAreaType1 [XmlType] public class OrganizationChartUserArea :UserAreaType1 { [XmlElement] public HcrChoiceSet DefaultChoices

我正在尝试扩展hr xml类型中的
UserArea
,ProcessOrganizationChartDataAreaType 我定义了类型
OrganizationChartUserArea
,它继承自
UserAreaType1

[XmlType]
    public class OrganizationChartUserArea :UserAreaType1
    {
        [XmlElement]
        public HcrChoiceSet DefaultChoicesSet { get; set; }

        [XmlElement]
        public HcrChoiceSet CurrentChoicesSet { get; set; }
    }
这还使用自定义类型HcrChoiceSet

 [XmlType]
    public class HcrChoiceSet
    {
        [XmlElement]
        public FreeFormEffectivePeriodType OngoingMeasurementPeriod { get; set; }

        [XmlElement]
        public FreeFormEffectivePeriodType OngoingStabilityPeriod { get; set; }

        [XmlElement]
        public FreeFormEffectivePeriodType OngoingAdministrationPeriod { get; set; }
    }
当我尝试搜索这个扩展类的实例时

 [TestMethod]
        public void SerializeOrganizationChart()
        {
            var organizationSerializer = new XmlSerializer(typeof(ProcessOrganizationChartType));
            var organizationWriter = new XmlTextWriter("C:\\ProcessOrganizationChart.xml", new UTF8Encoding());

            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "http://www.hr-xml.org/3");
            namespaces.Add("oa", "http://www.openapplications.org/oagis/9");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            namespaces.Add("schemaLocation", "http://www.hr-xml.org/3  ../Developer/BODs/ProcessOrganizationChart.xsd");

            organizationSerializer.Serialize(organizationWriter, processOrganizationChart, namespaces);

            Assert.Inconclusive("This is not a true unit test.");
        }
我听到一个错误说

试验方法 SomeNamespace.Tests.OrganizationChartTest.SerializeOrganizationChart 引发异常:System.InvalidOperationException:出现错误 正在生成XML文档。-->System.InvalidOperationException: 类型SomeNamespace.Tests.OrganizationChartUserArea不可用 预期。使用XmlInclude或SoapInclude属性指定类型 这不是静态已知的

我如何解决这个问题

 [TestInitialize]
        public void InitTest()
        {
            processOrganizationChart = new ProcessOrganizationChartType
                {
                    ApplicationArea = new ApplicationAreaType
                        {
                            CreationDateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                            BODID = new IdentifierType1 {Value = Guid.NewGuid().ToString()},
                        },
                    DataArea = new ProcessOrganizationChartDataAreaType
                        {
                            Process = new ProcessType
                                {
                                    ActionCriteria = new[]
                                        {
                                            new ActionCriteriaType
                                                {
                                                    ActionExpression = new[]
                                                        {
                                                            new ActionExpressionType
                                                                {
                                                                    actionCode = "Add",
                                                                    Value =
                                                                        "/ProcessOrganizationChart/DataArea/OrganizationChart"
                                                                }
                                                        }
                                                }
                                        }
                                },
                            OrganizationChart = new[]
                                {
                                    new OrganizationChartType
                                        {
                                            OrganizationUnit = new[]
                                                {
                                                    new OrgChartOrganizationUnitType
                                                        {
                                                            validFrom = "2013-01-01",
                                                            OrganizationUnitID = new IdentifierType2
                                                                {
                                                                    Value = "901"
                                                                },
                                                            ParentOrganizationUnit = new ParentOrganizationUnitType
                                                                {
                                                                    OrganizationUnitID = new IdentifierType2
                                                                        {
                                                                            Value = "900"
                                                                        }
                                                                },
                                                            RelatedOrganizationUnit = new[]
                                                                {
                                                                    new RelatedOrganizationUnitType
                                                                        {
                                                                            OrganizationUnitID = new IdentifierType2
                                                                                {
                                                                                    Value = "900"
                                                                                }
                                                                        }
                                                                },
                                                            OrganizationUnitName = new OrganizationNameType
                                                                {
                                                                    languageID = "en-US",
                                                                    Value = "SomeCompany"
                                                                },
                                                            TypeCode = new[]
                                                                {
                                                                    new CodeType1
                                                                        {
                                                                            listID = "ClientStatus",
                                                                            Value = "Active"
                                                                        },
                                                                    new CodeType1
                                                                        {
                                                                            listID = "ALEIndicator",
                                                                            Value = "True"
                                                                        },
                                                                    new CodeType1
                                                                        {
                                                                            listID = "EducationIndicator",
                                                                            Value = "False"
                                                                        },
                                                                    new CodeType1
                                                                        {
                                                                            listID = "MedicalBenefitsOffered",
                                                                            Value = "False",
                                                                        },
                                                                    new CodeType1
                                                                        {
                                                                            listID = "MedicalBenefitsStatusDate",
                                                                            Value = "2013-01-01"
                                                                        }
                                                                }
                                                            ,
                                                            UserArea = new OrganizationChartUserArea
                                                                {
                                                                    DefaultChoicesSet = new HcrChoiceSet
                                                                        {
                                                                            OngoingAdministrationPeriod
                                                                                = new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-31"
                                                                                            },
                                                                                        Duration = "P1M"
                                                                                    },
                                                                            OngoingMeasurementPeriod =
                                                                                new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2014-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2014-12-31"
                                                                                            },
                                                                                        Duration = "P12M"
                                                                                    },
                                                                            OngoingStabilityPeriod =
                                                                                new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-12-31"
                                                                                            },
                                                                                        Duration = "P12M"
                                                                                    }

                                                                        },
                                                                    CurrentChoicesSet = new HcrChoiceSet
                                                                        {
                                                                            OngoingAdministrationPeriod
                                                                                = new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-31"
                                                                                            }
                                                                                    },
                                                                            OngoingMeasurementPeriod =
                                                                                new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2014-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2014-12-31"
                                                                                            }
                                                                                    },
                                                                            OngoingStabilityPeriod =
                                                                                new FreeFormEffectivePeriodType
                                                                                    {
                                                                                        StartDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-01-01"
                                                                                            },
                                                                                        EndDate = new FreeFormDateType
                                                                                            {
                                                                                                FormattedDateTime =
                                                                                                    "2015-12-31"
                                                                                            }
                                                                                    }

                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
        }
OrgChartUnitType

namespace HrXml.v3_2_1.Types.HrXml
{
    [Serializable]
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    [GeneratedCode("xsd", "4.0.30319.17929")]
    [XmlRoot("OrganizationUnit", Namespace = "http://www.hr-xml.org/3", IsNullable = false)]
    [XmlType(Namespace = "http://www.hr-xml.org/3")]
    public class OrgChartOrganizationUnitType
    {
        public OrgChartOrganizationUnitType();

        [XmlElement("AdditionalItem")]
        public AdditionalItemType[] AdditionalItem { get; set; }
        [XmlElement("MemberPosition")]
        public MemberPositionType[] MemberPosition { get; set; }
        public CodeType1 OrganizationLevelCode { get; set; }
        public IdentifierType2 OrganizationUnitID { get; set; }
        public TextType1 OrganizationUnitName { get; set; }
        public ParentOrganizationUnitType ParentOrganizationUnit { get; set; }
        [XmlElement("RelatedOrganizationUnit")]
        public RelatedOrganizationUnitType[] RelatedOrganizationUnit { get; set; }
        [XmlIgnore]
        public AdditionalItemType SingleAdditionalItem { get; set; }
        [XmlIgnore]
        public MemberPositionType SingleMemberPosition { get; set; }
        [XmlIgnore]
        public RelatedOrganizationUnitType SingleRelatedOrganizationUnit { get; set; }
        [XmlIgnore]
        public CodeType1 SingleTypeCode { get; set; }
        [XmlElement("TypeCode")]
        public CodeType1[] TypeCode { get; set; }
        public UserAreaType1 UserArea { get; set; }
        [XmlAttribute]
        public string validFrom { get; set; }
        [XmlAttribute]
        public string validTo { get; set; }
    }
}

在基类中使用XmlInclude属性:

[XmlInclude(typeof(OrganizationChartUserArea))]
class public UserAreaType1
{
 [...]

在基类中使用XmlInclude属性:

[XmlInclude(typeof(OrganizationChartUserArea))]
class public UserAreaType1
{
 [...]

由于您无权访问类来修改和添加
xmlclude
属性,因此应通知
XmlSerializer
使用构造函数重载进行序列化的其他已知类型:

var organizationSerializer = new XmlSerializer(typeof(ProcessOrganizationChartType), new[]{typeof(OrganizationChartUserArea)});

这指示
XmlSerializer
期望并生成自定义子类的序列化信息。通常,
XmlSerializer
只查看编译时在序列化的类上定义的类型。在这种情况下,
OrgChartOrganizationUnitType
指定了一种类型
UserAreaType1
,因此只需要特定的类,并生成序列化信息。通过告诉
XmlSerializer
也期望
OrganizationChartUserArea
在传递该类型的对象时,它应该不会失败。

因为您无权访问类来修改和添加
XmlInclude
属性,而是通知
XmlSerializer
使用构造函数重载进行序列化的其他已知类型:

var organizationSerializer = new XmlSerializer(typeof(ProcessOrganizationChartType), new[]{typeof(OrganizationChartUserArea)});

这指示
XmlSerializer
期望并生成自定义子类的序列化信息。通常,
XmlSerializer
只查看编译时在序列化的类上定义的类型。在这种情况下,
OrgChartOrganizationUnitType
指定了一种类型
UserAreaType1
,因此只需要特定的类,并生成序列化信息。通过告诉
XmlSerializer
也期望
OrganizationChartUserArea
它在传递该类型的对象时应该不会失败。

您可以发布用于序列化的代码吗(请说明如何使用
XmlSerializer
?很可能对某些基本类型进行序列化/反序列化,但需要包括预期拥有的所有已知子类(例如
OrganizationChartUserArea
)编辑:另外,如果
OrganizationChartUserArea
不是要直接序列化/反序列化的对象,如果它实际上是要序列化的更高级别类的属性,也可以发布该属性。@ChrisSinclair我已经添加了它above@ChrisSinclair序列化ProcessOrganizationChartType没有问题,只是在我尝试时扩展使用我的新类型。ProcessOrganizationChartType是由hr xml定义的。您可以在此处看到它。您尝试序列化
ProcessOrganizationChartType
但发布了
OrganizationChartUserArea
的代码的原因是什么?您可以发布
ProcessOrganizationChartType
,还是尝试序列化Instance
OrganizationChartUserArea
是否使用您在上面发布的确切序列化代码(与实例化
XmlSerializer(typeof(ProcessOrganizationChartType))
完全相同?@ChrisSinclair我添加了更多代码,让您更清楚。您可以发布用于序列化的代码吗(请说明如何使用
XmlSerializer
?很可能对某些基本类型进行序列化/反序列化,但需要包括预期拥有的所有已知子类(例如
OrganizationChartUserArea
)编辑:另外,如果
OrganizationChartUserArea
不是要直接序列化/反序列化的对象,如果它实际上是要序列化的更高级别类的属性,也可以发布该属性。@ChrisSinclair我已经添加了它above@ChrisSinclair序列化ProcessOrganizationChartType没有问题,只是在我尝试时扩展使用我的新类型。ProcessOrganizationChartType是由hr xml定义的。您可以在此处看到它。您尝试序列化
ProcessOrganizationChartType
但发布了
OrganizationChartUserArea
的代码的原因是什么?您可以发布
ProcessOrganizationChartType
,还是尝试序列化Instance使用您在上面发布的确切序列化代码(与实例化
XmlSerializer(typeof(ProcessOrganizationChartType))创建
OrganizationChartUserArea
)?@ChrisSinclair我添加了更多代码,让您更清楚。这不是一个我也有权访问的类,我引用的是一个dll。这不是一个我也有权访问的类,我引用的是一个dll