无法反序列化属性

时间:2017-07-05 05:35:10

标签: c# xml wcf serialization attributes

我在某些XML中有以下元素,我需要反序列化:

<settlementLineItem number="1">
    <amountPaid>38250.89</amountPaid>
    <originalAmount>42501.00</originalAmount>
    <adjustmentAndDiscount>
        <amount>850.02</amount>
        <adjustmentReason>
            <messageReason>XG 2.000 % Distribution Allow.</messageReason>
            <sourceCode>TNT</sourceCode>
        </adjustmentReason>
        <alternateAdjustmentReference>
            <alternateAdjustmentReferenceType>DOC_NO</alternateAdjustmentReferenceType>
            <identification>2200527269</identification>
        </alternateAdjustmentReference>
    </adjustmentAndDiscount>
    <adjustmentAndDiscount>
        <amount>2125.05</amount>
        <adjustmentReason>
            <messageReason>XI 5.000 % Incentive Discount</messageReason>
            <sourceCode>TNT</sourceCode>
        </adjustmentReason>
        <alternateAdjustmentReference>
            <alternateAdjustmentReferenceType>DOC_NO</alternateAdjustmentReferenceType>
            <identification>2200528098</identification>
        </alternateAdjustmentReference>
    </adjustmentAndDiscount>
    <adjustmentAndDiscount>
        <amount>1062.53</amount>
        <adjustmentReason>
            <messageReason>XF 2.500 % Advertising Allow.</messageReason>
            <sourceCode>TNT</sourceCode>
        </adjustmentReason>
        <alternateAdjustmentReference>
            <alternateAdjustmentReferenceType>DOC_NO</alternateAdjustmentReferenceType>
            <identification>2200529157</identification>
        </alternateAdjustmentReference>
    </adjustmentAndDiscount>
    <adjustmentAndDiscount>
        <amount>212.51</amount>
        <adjustmentReason>
            <messageReason>XO 0.500 % Training &amp; Dev. Cost</messageReason>
            <sourceCode>TNT</sourceCode>
        </adjustmentReason>
        <alternateAdjustmentReference>
            <alternateAdjustmentReferenceType>DOC_NO</alternateAdjustmentReferenceType>
            <identification>2200529467</identification>
        </alternateAdjustmentReference>
    </adjustmentAndDiscount>
    <invoice creationDateTime="2017-06-28">
        <uniqueCreatorIdentification>4912115908</uniqueCreatorIdentification>
        <contentOwner>
            <gln>6001000000001</gln>
        </contentOwner>
        <invoiceType>CREDIT_NOTE </invoiceType>
    </invoice>
    <settlementEntity entityType="SITE">
        <partyIdentification>
            <gln>6001007031336</gln>
        </partyIdentification>
    </settlementEntity>
</settlementLineItem>

我使用这个类来表示该特定元素:

[XmlRoot(ElementName = "settlementLineItem")]
public class SettlementLineItem
{
    [XmlAttribute(AttributeName = "number")]
    public string Number { get; set; }

    // Standard data
    [XmlElement(ElementName = "amountPaid")]
    public string AmountPaid { get; set; }
    [XmlElement(ElementName = "originalAmount")]
    public string OriginalAmount { get; set; }
    [XmlElement(ElementName = "invoice")]
    public Invoice Invoice { get; set; }
    [XmlElement(ElementName = "settlementEntity")]
    public SettlementEntity SettlementEntity { get; set; }
    [XmlElement(ElementName = "adjustmentAndDiscount")]
    public List<AdjustmentAndDiscount> AdjustmentAndDiscount { get; set; }

    // Exetension data
    [XmlElement(ElementName = "documentType")]
    public string DocumentType { get; set; }
    [XmlElement(ElementName = "documentNumber")]
    public string DocumentNumber { get; set; }
    [XmlElement(ElementName = "itemText")]
    public string ItemText { get; set; }
}

执行反序列化的代码:

private Message DeserializeXml(string xml)
{
    var serializer = new XmlSerializer(typeof(Message));
    using (var reader = new StringReader(xml))
    {
        return (Message)serializer.Deserialize(reader);
    }
}

所有元素都可以反复序列化而不会出现任何问题,但Number属性始终为null

我在文档中的所有属性都看到了这个问题,我试图反序列化。

有谁知道为什么会这样?

请注意,我没有包含整个xml消息(它很大,类型为Message),但这应该是足够的信息,因为反序列化本身是成功的(只是在属性时)

谢谢!

修改

InvoiceSettlementEntityAdjustmentAndDiscount

的代码
[XmlRoot(ElementName = "invoice")]
public class Invoice
{
    [XmlElement(ElementName = "uniqueCreatorIdentification")]
    public string UniqueCreatorIdentification { get; set; }
    [XmlElement(ElementName = "contentOwner")]
    public ContentOwner ContentOwner { get; set; }
    [XmlElement(ElementName = "invoiceType")]
    public string InvoiceType { get; set; }
}

[XmlRoot(ElementName = "settlementEntity")]
public class SettlementEntity
{
    [XmlElement(ElementName = "partyIdentification")]
    public PartyIdentification PartyIdentification { get; set; }
}

[XmlRoot(ElementName = "adjustmentAndDiscount")]
public class AdjustmentAndDiscount
{
    [XmlElement(ElementName = "adjustmentReason")]
    public AdjustmentReason AdjustmentReason { get; set; }

    // LineItem data
    [XmlElement(ElementName = "amount")]
    public string Amount { get; set; }
    [XmlElement(ElementName = "alternateAdjustmentReference")]
    public AlternateAdjustmentReference AlternateAdjustmentReference { get; set; }

    // Summary data
    [XmlElement(ElementName = "amountTotal")]
    public string AmountTotal { get; set; }
    [XmlElement(ElementName = "amountNet")]
    public string AmountNet { get; set; }
    [XmlElement(ElementName = "amountVAT")]
    public string AmountVAT { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我在名为“XMLFile1.xml”的应用程序中创建了一个xml文件,并将xml字符串复制到“XMLFile1.xml”文件中。

<强> CODE

using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace DeserializeXMLConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlString = File.ReadAllText(@"D:\My Apps\Console Applications\DeserializeXMLConsoleApp\DeserializeXMLConsoleApp\XMLFile1.xml");
            var serializer = new XmlSerializer(typeof(SettlementLineItem));
            using (var reader = new StringReader(xmlString))
            {
                var finalResult =  (SettlementLineItem)serializer.Deserialize(reader);
            }
            Console.Read();
        }
    }

    [XmlRoot(ElementName = "adjustmentReason")]
    public class AdjustmentReason
    {
        [XmlElement(ElementName = "messageReason")]
        public string MessageReason { get; set; }
        [XmlElement(ElementName = "sourceCode")]
        public string SourceCode { get; set; }
    }

    [XmlRoot(ElementName = "alternateAdjustmentReference")]
    public class AlternateAdjustmentReference
    {
        [XmlElement(ElementName = "alternateAdjustmentReferenceType")]
        public string AlternateAdjustmentReferenceType { get; set; }
        [XmlElement(ElementName = "identification")]
        public string Identification { get; set; }
    }

    [XmlRoot(ElementName = "adjustmentAndDiscount")]
    public class AdjustmentAndDiscount
    {
        [XmlElement(ElementName = "amount")]
        public string Amount { get; set; }
        [XmlElement(ElementName = "adjustmentReason")]
        public AdjustmentReason AdjustmentReason { get; set; }
        [XmlElement(ElementName = "alternateAdjustmentReference")]
        public AlternateAdjustmentReference AlternateAdjustmentReference { get; set; }
    }

    [XmlRoot(ElementName = "contentOwner")]
    public class ContentOwner
    {
        [XmlElement(ElementName = "gln")]
        public string Gln { get; set; }
    }

    [XmlRoot(ElementName = "invoice")]
    public class Invoice
    {
        [XmlElement(ElementName = "uniqueCreatorIdentification")]
        public string UniqueCreatorIdentification { get; set; }
        [XmlElement(ElementName = "contentOwner")]
        public ContentOwner ContentOwner { get; set; }
        [XmlElement(ElementName = "invoiceType")]
        public string InvoiceType { get; set; }
        [XmlAttribute(AttributeName = "creationDateTime")]
        public string CreationDateTime { get; set; }
    }

    [XmlRoot(ElementName = "partyIdentification")]
    public class PartyIdentification
    {
        [XmlElement(ElementName = "gln")]
        public string Gln { get; set; }
    }

    [XmlRoot(ElementName = "settlementEntity")]
    public class SettlementEntity
    {
        [XmlElement(ElementName = "partyIdentification")]
        public PartyIdentification PartyIdentification { get; set; }
        [XmlAttribute(AttributeName = "entityType")]
        public string EntityType { get; set; }
    }

    [XmlRoot(ElementName = "settlementLineItem")]
    public class SettlementLineItem
    {
        [XmlElement(ElementName = "amountPaid")]
        public string AmountPaid { get; set; }
        [XmlElement(ElementName = "originalAmount")]
        public string OriginalAmount { get; set; }
        [XmlElement(ElementName = "adjustmentAndDiscount")]
        public List<AdjustmentAndDiscount> AdjustmentAndDiscount { get; set; }
        [XmlElement(ElementName = "invoice")]
        public Invoice Invoice { get; set; }
        [XmlElement(ElementName = "settlementEntity")]
        public SettlementEntity SettlementEntity { get; set; }
        [XmlAttribute(AttributeName = "number")]
        public string Number { get; set; }
    }
}

输出&gt;请参见Number属性值

enter image description here

如果它解决了您的问题,请检查并将其还原。