2016-06-11 3 views
0

XML 문서를 deserialize하려고하는데 사용하는 코드가 매번 Null 값을 반환합니다.XML 객체에 직렬화 해제하면 null 값이 반환됩니다.

나는이

<?xml version="1.0" encoding="utf-8"?> 
<RegistrationOpenData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.gov"> 
<Description>Registration data is collected by ABC XYZ</Description> 
<InformationURL>http://www.example.com/html/hpd/property-reg-unit.shtml</InformationURL> 
<SourceAgency>ABC Department of Housing</SourceAgency> 
<SourceSystem>PREMISYS</SourceSystem> 
<StartDate>2016-02-29T00:03:06.642772-05:00</StartDate> 
<EndDate i:nil="true" /> 
<Registrations> 
<Registration xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<RegistrationID>108260</RegistrationID> 
<BuildingID>4731</BuildingID> 
</Registration> 
</Registrations> 
</RegistrationOpenData> 

같은 XML이 직렬화해야, I 클래스

using System.Xml.Serialization; 
using System.Xml; 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.gov")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://example.gov", IsNullable=true)] 
public partial class Registration : InfoClass { 

    private long registrationIDField; 
    private bool registrationIDFieldSpecified; 
    private System.Nullable<long> buildingIDField; 
    private bool buildingIDFieldSpecified; 

    public long RegistrationID 
{ 
    get 
    { 
     return this.registrationIDField; 
    } 
    set 
    { 
     this.registrationIDField = value; 
    } 
} 
[System.Xml.Serialization.XmlIgnoreAttribute()] 
public bool RegistrationIDSpecified { 
    get { 
     return this.registrationIDFieldSpecified; 
    } 
    set { 
     this.registrationIDFieldSpecified = value; 
    } 
} 

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] 
public System.Nullable<long> BuildingID { 
    get { 
     return this.buildingIDField; 
    } 
    set { 
     this.buildingIDField = value; 
    } 
} 

[System.Xml.Serialization.XmlIgnoreAttribute()] 
public bool BuildingIDSpecified { 
    get { 
     return this.buildingIDFieldSpecified; 
    } 
    set { 
     this.buildingIDFieldSpecified = value; 
    } 
} 

내가 사용하고있는 코드를 여기에

public void Test() 
    { 

     Registration RegistrationVal = null; 
     var xRoot = new XmlRootAttribute(); 
     xRoot.ElementName = "RegistrationOpenData"; 
     xRoot.Namespace = "http://services.hpd.gov"; 
     xRoot.IsNullable = true; 
     var serializer = new XmlSerializer(typeof(Registration), xRoot); 
     using (TextReader reader = new StreamReader(@"D:\sample.xml")) 
      { 
       RegistrationVal = (Registration)serializer.Deserialize(reader); 
      } 
} 

가 있습니다 만든 항상 Null 값을 반환합니다. 미리 도움을 주셔서 감사합니다.

+0

디버깅 할 때 예외가 표시됩니까? –

+0

아니요, TextBox의 RegistrationID 값을 얻으려고했는데 0을 인쇄합니다 – Hanumendra

+0

'InfoClass'에 어떤 코드가 있습니까? xml 속성이 있습니까? –

답변

1

등록 목록이있어 xml에 문제가 있습니다. <Registration><Registrations> 태그를 제거하면 작동합니다. RegistrationRegistrations이 필요합니다.이 경우 Lists으로 작업해야하기 때문입니다.

당신은 ListRegistration의 요소를 개최 이는 자신의 클래스 Registrations이 예제 (Deserializing nested xml into C# objects)

에서처럼을하고 만들 수 있습니다.

이 코드를 사용하면 작동합니다.

[XmlRoot("RegistrationOpenData")] 
public class RegistrationOpenData 
{ 
    [XmlElement("Registrations")] 
    public Registrations Regs { get; set; } 
} 

Registrations을 : 수퍼 클래스를 작성

[XmlRoot("Registrations")] 
public class Registrations 
{ 
    [XmlElement("Registration")] 
    public List<Registration> Regs { get; set; } 
} 

Registration는 이전과 동일해야합니다. 주 기능이 다음과 같이 바뀌어야합니다.

static void Main(string[] args) 
{ 
     RegistrationOpenData RegistrationVal = null; 
     var xRoot = new XmlRootAttribute(); 
     xRoot.ElementName = "RegistrationOpenData"; 
     xRoot.Namespace = "http://services.hpd.gov"; 
     xRoot.IsNullable = true; 
     var serializer = new XmlSerializer(typeof(RegistrationOpenData), xRoot); 
     using (TextReader reader = new StreamReader(@"D:\sample.xml")) 
     { 
      RegistrationVal = (RegistrationOpenData)serializer.Deserialize(reader); 
     } 
} 
+0

수동으로 해당 태그를 제거 할 수 없으며 코드를 통해 해당 태그를 제거해야합니까? – Hanumendra

+0

당신은'Registrations' 클래스를 사용하여 답안에서 설명한 것처럼 할 수 있습니다. –

+0

@Hanumendra가 솔루션 작업을 수행합니까? –

관련 문제