2013-07-09 6 views
1

나는이 또한 사용자 정의 유형을 사용 UserAreaType1는 사용자 정의 유형을 직렬화 할 수 없습니다

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

     [XmlElement] 
     public HcrChoiceSet CurrentChoicesSet { get; set; } 
    } 

에서 상속 유형 OrganizationChartUserArea을 정의 ProcessOrganizationChartDataAreaType 인사-xml 유형의 UserArea을 확장하기 위해 노력하고있어 HcrChoiceSet

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

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

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

나는이 확장 된 클래스의 인스턴스를 searlize 할 때

[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."); 
     } 
System.InvalidOperationException :

나는

시험 방법 SomeNamespace.Tests.OrganizationChartTest.SerializeOrganizationChart 가 예외를 던진 말 오류가 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; } 
    } 
} 
당신의 기본 클래스
+0

당신이 직렬화하는 데 사용하는 코드를 게시 할 수 (예를 들어 당신은 몇 가지 기본 유형을 역 직렬화은'XmlSerializer'? 대부분의 경우를/직렬화 사용하고 있지만, 같은 당신이 기대 알려진 모든 서브 클래스를 (포함해야하는지 은'OrganizationChartUserArea') 편집 : 실제로 당신이 직렬화하는 상위 클래스의 속성 인 경우 OrganizationChartUserArea'가 직접 직렬화 된 객체/역 직렬화 아닌'경우에도, 어쩌면뿐만 아니라 해당 게시물 –

+0

을 @. ChrisSinclair 내가 ProcessOrganizationChartType 직렬화 문제를 가지고하지 –

+0

@ChrisSinclair 이상을 추가 한 자사 내 새로운 유형을 사용하여 확장하려고 할 때 ProcessOrganizationChartType은 HR-XML로 정의 된 경우에만 당신이 그것을 볼 수 있습니다 여기에 HTTP :. //neptune.pilotfish-net .com/HRXML/index.html? page = http : //neptune.pilotfish-net.com/HRXML/model/Format.ProcessOrganizationChart.DataArea.html –

답변

2

대신 알려주 XmlSerializernew XmlSerializer(Type, Type[]) 생성자 오버로드를 사용하여 직렬화 할 것으로 예상되는 추가 알려진 유형의 수 :

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

이렇게하면 XmlSerializer에 사용자 지정 하위 클래스의 serialization 정보가 필요하고 생성되도록 지시합니다. 일반적으로 XmlSerializer은 직렬화 된 클래스에서 컴파일 타임에 정의 된 유형을 조사합니다. 이 경우, 그렇게 OrgChartOrganizationUnitType 해당 특정 클래스가 예상 UserAreaType1의 타입을 지정하고, 순차 나열 정보를 생성한다. XmlSerializer으로 말하면OrganizationChartUserArea이라고 예상하여 해당 유형의 객체가 전달 될 때 실패하지는 않습니다.

+0

노력에 감사드립니다. –

2

사용 XmlInclude 특성 : 당신이 수정 XmlInclude 속성을 추가 할 수있는 클래스에 대한 액세스 권한이없는 때문에

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

+1 아 닌자! 나는 이것을 쓰고 있었다! 잘 했어! –

+0

이것은 내가 액세스 할 수있는 클래스가 아니므로 DLL을 참조합니다. –

관련 문제