Amazon XML反序列化processingreport并获取空值

时间:2014-04-16 11:48:49

标签: c# xml amazon-web-services xsd

我正在尝试将xml文件读入类中以返回其值。这些类是通过Amazon的xsd文件中的xsd.exe创建的。用于尝试和反序列化的代码是:

string FeedSubmissionID = "8395421750";
FileStream responseFile = new FileStream(("d:/Amazon/SubmissionResult-" +  FeedSubmissionID + ".txt"), FileMode.Open, FileAccess.Read);           
ProcessingReport _report = new ProcessingReport();
XmlSerializer _des = new XmlSerializer(typeof(ProcessingReport), new XmlRootAttribute("AmazonEnvelope"));
_report = (ProcessingReport)_des.Deserialize(responseFile);

我在ProcessingReport中使用的代码是:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://mws.amazonaws.com/doc/2009-01-01/", IsNullable = false)]
public partial class ProcessingReport
{

    private string documentTransactionIDField;

    private ProcessingReportStatusCode statusCodeField;

    private ProcessingReportProcessingSummary processingSummaryField;

    private ProcessingReportResult[] resultField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "positiveInteger")]
    public string DocumentTransactionID
    {
        get
        {
            return this.documentTransactionIDField;
        }
        set
        {
            this.documentTransactionIDField = value;
        }
    }

    /// <remarks/>
    public ProcessingReportStatusCode StatusCode
    {
        get
        {
            return this.statusCodeField;
        }
        set
        {
            this.statusCodeField = value;
        }
    }

    /// <remarks/>

    public ProcessingReportProcessingSummary ProcessingSummary
    {
        get
        {
            return this.processingSummaryField;
        }
        set
        {
            this.processingSummaryField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Result")]
    public ProcessingReportResult[] Result
    {
        get
        {
            return this.resultField;
        }
        set
        {
            this.resultField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ProcessingReportStatusCode
{

    /// <remarks/>
    Complete,

    /// <remarks/>
    Processing,

    /// <remarks/>
    Rejected,
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ProcessingReportProcessingSummary
{

    private string messagesProcessedField;

    private string messagesSuccessfulField;

    private string messagesWithErrorField;

    private string messagesWithWarningField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "nonNegativeInteger")]
    public string MessagesProcessed
    {
        get
        {
            return this.messagesProcessedField;
        }
        set
        {
            this.messagesProcessedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "nonNegativeInteger")]
    public string MessagesSuccessful
    {
        get
        {
            return this.messagesSuccessfulField;
        }
        set
        {
            this.messagesSuccessfulField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "nonNegativeInteger")]
    public string MessagesWithError
    {
        get
        {
            return this.messagesWithErrorField;
        }
        set
        {
            this.messagesWithErrorField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "nonNegativeInteger")]
    public string MessagesWithWarning
    {
        get
        {
            return this.messagesWithWarningField;
        }
        set
        {
            this.messagesWithWarningField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ProcessingReportResult
{

    private string messageIDField;

    private ProcessingReportResultResultCode resultCodeField;

    private string resultMessageCodeField;

    private string resultDescriptionField;

    private ProcessingReportResultAdditionalInfo additionalInfoField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "nonNegativeInteger")]
    public string MessageID
    {
        get
        {
            return this.messageIDField;
        }
        set
        {
            this.messageIDField = value;
        }
    }

    /// <remarks/>
    public ProcessingReportResultResultCode ResultCode
    {
        get
        {
            return this.resultCodeField;
        }
        set
        {
            this.resultCodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "positiveInteger")]
    public string ResultMessageCode
    {
        get
        {
            return this.resultMessageCodeField;
        }
        set
        {
            this.resultMessageCodeField = value;
        }
    }

    /// <remarks/>
    public string ResultDescription
    {
        get
        {
            return this.resultDescriptionField;
        }
        set
        {
            this.resultDescriptionField = value;
        }
    }

    /// <remarks/>
    public ProcessingReportResultAdditionalInfo AdditionalInfo
    {
        get
        {
            return this.additionalInfoField;
        }
        set
        {
            this.additionalInfoField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ProcessingReportResultResultCode
{

    /// <remarks/>
    Error,

    /// <remarks/>
    Warning,
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ProcessingReportResultAdditionalInfo
{

    private string sKUField;

    private string fulfillmentCenterIDField;

    private string amazonOrderIDField;

    private string amazonOrderItemCodeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")]
    public string SKU
    {
        get
        {
            return this.sKUField;
        }
        set
        {
            this.sKUField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")]
    public string FulfillmentCenterID
    {
        get
        {
            return this.fulfillmentCenterIDField;
        }
        set
        {
            this.fulfillmentCenterIDField = value;
        }
    }

    /// <remarks/>
    public string AmazonOrderID
    {
        get
        {
            return this.amazonOrderIDField;
        }
        set
        {
            this.amazonOrderIDField = value;
        }
    }

    /// <remarks/>
    public string AmazonOrderItemCode
    {
        get
        {
            return this.amazonOrderItemCodeField;
        }
        set
        {
            this.amazonOrderItemCodeField = value;
        }
    }
}

最后是xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
    <DocumentVersion>1.02</DocumentVersion>
    <MerchantIdentifier>??????????????</MerchantIdentifier>
</Header>
<MessageType>ProcessingReport</MessageType>
<Message>
    <MessageID>1</MessageID>
    <ProcessingReport>
        <DocumentTransactionID>8395421750</DocumentTransactionID>
        <StatusCode>Complete</StatusCode>
        <ProcessingSummary>
            <MessagesProcessed>1</MessagesProcessed>
            <MessagesSuccessful>0</MessagesSuccessful>
            <MessagesWithError>1</MessagesWithError>
            <MessagesWithWarning>0</MessagesWithWarning>
        </ProcessingSummary>
        <Result>
            <MessageID>1</MessageID>
            <ResultCode>Error</ResultCode>
            <ResultMessageCode>5000</ResultMessageCode>
            <ResultDescription>XML Parsing Error at Line 22, Column 24: cvc-minLength-valid: Value &apos;&apos; with length = &apos;0&apos; is not facet-valid with respect to minLength &apos;1&apos; for type &apos;LongStringNotNull&apos;.</ResultDescription>
            <AdditionalInfo>
                <SKU>MySKU</SKU>
            </AdditionalInfo>
        </Result>
    </ProcessingReport>
</Message>
</AmazonEnvelope>

返回的_report对象具有所有空值。

谁能看到我哪里出错?

1 个答案:

答案 0 :(得分:0)

问题解决了。

需要做的是,首先需要将亚马逊饲料的结果流保存到文件中,然后访问创建的文件并读入该类。而我试图直接从流中读取结果。

希望这有助于其他人