需要帮助使用c#datacontract deserializer

时间:2015-10-20 04:11:03

标签: c# xml datacontract xml-deserialization

我正在尝试使用Datacontract反序列化器反序列化XMl。

 <?xml version="1.0" encoding="utf-8"?>
 <Invoice>
 <Samples>
    <Sample>
      <AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
      <AccountNumber>55761598808</AccountNumber>
    </Sample>
     <Sample>
       <AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
       <AccountNumber>55761598808</AccountNumber>
    </Sample>
  </Samples>
</Invoice>

我已经定义了以下类来反序列化

        [DataContract(Name = "Sample", Namespace = "")]
         public class Sample
         {
             [DataMember(Name = "AccountId")]
             public string AccountId { get; set; }
             [DataMember(Name = "AccountNumber")]
             public string AccountNumber { get; set; }
         }

         [DataContract(Name = "Samples", Namespace = "")]
         public class Samples
         {
             [DataMember(Name = "Sample")]
             public List<Sample> Sample { get; set; }
         }

         [DataContract(Name = "Invoice", Namespace = "")]
         public class Invoice
         {
             [DataMember(Name = "Samples")]
             public Samples Samples { get; set; }
         }

   And the test method to deserialize
    [TestMethod]
    public void SampleXmlTest()
    {  
        string fileName = @"C:\Users\SJuluru\Desktop\Sample      XML\Samplexml_Modified.xml";
        FileStream fs = new FileStream(fileName,  FileMode.Open);
        XmlDictionaryReader reader =
        XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
        DataContractSerializer ser = new DataContractSerializer(typeof(CommonAvidXmlDto.Invoice));
        CommonAvidXmlDto.Invoice deserializedInvoice =        (CommonAvidXmlDto.Invoice)ser.ReadObject(reader, true);
        reader.Close();
        fs.Close();
     }

当我在调试模式下运行该方法时,deserializedInvoice对象变为null,并且没有复制XML中的数据。请建议我解决这个问题的解决方案

1 个答案:

答案 0 :(得分:0)

查看反序列化问题的最简单方法是序列化类的示例,并将结果与​​预期结果进行比较。例如,如果我按如下方式创建Invoice

        var invoice = new Invoice 
        { 
            Samples = new Samples { Sample = new List<Sample> { new Sample { AccountId = "1e547ae6-9a6d-d18f-958b-22000b83a845", AccountNumber = "55761598808" } } } 
        };

这序列化为以下XML:

<Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Samples>
        <Sample>
            <Sample>
                <AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
                <AccountNumber>55761598808</AccountNumber>
            </Sample>
        </Sample>
    </Samples>
</Invoice>

正如您所看到的,有<Sample>嵌套的额外级别。因此,您的中间Samples类是不必要的。您可以使用DataContractSerializer反序列化XML,只需以下两个类:

[DataContract(Name = "Sample", Namespace = "")]
public class Sample
{
    [DataMember(Name = "AccountId")]
    public string AccountId { get; set; }
    [DataMember(Name = "AccountNumber")]
    public string AccountNumber { get; set; }
}

[DataContract(Name = "Invoice", Namespace = "")]
public class Invoice
{
    [DataMember(Name = "Samples")]
    public List<Sample> Samples { get; set; }
}