1

我正在尝试扩展 我定义的继承自UserArea的类型的 hr-xml 类型ProcessOrganizationChartDataAreaTypeOrganizationChartUserAreaUserAreaType1

[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"
                                                                                            }
                                                                                    }

                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
        }

组织结构图单位类型

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; }
    }
}
4

2 回答 2

2

在您的基类中使用 XmlInclude 属性:

[XmlInclude(typeof(OrganizationChartUserArea))]
class public UserAreaType1
{
 [...]
于 2013-07-09T20:27:47.060 回答
2

由于您无权访问该类来修改和添加XmlInclude属性,因此请使用构造函数重载通知XmlSerializer其他已知类型以期望序列化:new XmlSerializer(Type, Type[])

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

这指示XmlSerializer期望并为您的自定义子类生成序列化信息。通常XmlSerializer只查看在编译时对序列化的类定义的类型。在这种情况下,OrgChartOrganizationUnitType指定一个类型,UserAreaType1以便仅预期特定的类并生成序列化信息。通过告诉XmlSerializer期望OrganizationChartUserArea在传递该类型的对象时希望它不会失败。

于 2013-07-09T21:05:16.820 回答