XML序列化问题-XmlRoot节点重复

本文关键字:节点 -XmlRoot 序列化 问题 XML | 更新日期: 2023-09-27 18:26:17

我使用的是第三方XML API,它返回XML响应。使用API,我正在尝试导入订单。因此,当我用这个API导入订单时,可能会出现两种情况:

  • 成功-例如xml响应:http://pastebin.com/ZcvvYMX6
  • 失败-例如xml响应:http://pastebin.com/iVjqMKAR

我正在做的是这个API的包装器,我有一个名为ImportOrders的方法。在这个方法中,我试图将xml反序列化为一个对象,以便返回一个公共响应。

例如;我将有一个常见的响应DTO类,如下所示:

public class ImportOrderResponse
{
    bool IsError { get; set; }
    string ErrorMsg { get; set; }
    string Result { get; set; }
}

CCD_ 2方法将有条件地返回响应,如下所示:

if (apiResult.Contains("importFailures"))
{
    // todo: deserialize
    return new ImportOrderResponse()
    {
        IsError = true,
        ErrorMsg = "todo - get failureMessage"
    };
}
else
{
    // todo: deserialize
    return new ImportOrderResponse()
    {
        IsError = false,
        Result = "todo - haven't worked out what i need to return"
    };
}

使用此网站http://xmltocsharp.azurewebsites.net我生成了用于反序列化的类,如下所示:

ImportSuccess.cs

[XmlRoot(ElementName = "import")]
public class ImportSuccess
{
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
    [XmlAttribute(AttributeName = "operation")]
    public string Operation { get; set; }
    [XmlAttribute(AttributeName = "entity")]
    public string Entity { get; set; }
    [XmlAttribute(AttributeName = "externalReference")]
    public string ExternalReference { get; set; }
    [XmlAttribute(AttributeName = "item")]
    public string Item { get; set; }
    [XmlAttribute(AttributeName = "queryTime")]
    public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
    [XmlElement(ElementName = "import")]
    public List<ImportSuccess> Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
    [XmlElement(ElementName = "importSuccesses")]
    public ImportSuccesses ImportSuccesses { get; set; }
    [XmlElement(ElementName = "importFailures")]
    public string ImportFailures { get; set; }
    [XmlElement(ElementName = "importDuplicates")]
    public string ImportDuplicates { get; set; }
}

ImportFailure.cs

[XmlRoot(ElementName = "import")]
public class ImportFailure
{
    [XmlElement(ElementName = "failureMessage")]
    public string FailureMessage { get; set; }
    [XmlElement(ElementName = "failureDetail")]
    public string FailureDetail { get; set; }
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
    [XmlAttribute(AttributeName = "operation")]
    public string Operation { get; set; }
    [XmlAttribute(AttributeName = "externalReference")]
    public string ExternalReference { get; set; }
    [XmlAttribute(AttributeName = "queryTime")]
    public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
    [XmlElement(ElementName = "import")]
    public ImportFailure Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
    [XmlElement(ElementName = "importSuccesses")]
    public string ImportSuccesses { get; set; }
    [XmlElement(ElementName = "importFailures")]
    public ImportFailures ImportFailures { get; set; }
    [XmlElement(ElementName = "importDuplicates")]
    public string ImportDuplicates { get; set; }
}

但现在我遇到了一个问题。无论是成功与否;故障响应包含节点CCD_ 3,但具有不同的字段/元素名称。

现在我出现了一个错误,因为我有一个重复的ImportResult类。

我该如何解决这个问题?处理这种情况的正确方法是什么?

XML序列化问题-XmlRoot节点重复

我觉得我很傻。。。我做这件事的方法不对。

我所做的是;

  • 使用正确的数据调用api以获得成功响应,获取xml并创建一个类
  • 使用错误/坏数据调用api以获得失败响应,获取xml并尝试创建另一个类

经过一番挖掘,我意识到反应结构非常接近成功/失败。

所以,我调用了api,而不是发送一个订单;我发送了3个特定场景的订单。这导致了一个像这样的XML响应:

http://pastebin.com/sNgWAwq1

现在我有了一个很好的XML结构(成功/失败和重复)。使用这个XML,我生成了以下类(用于取消序列化):

[XmlRoot(ElementName = "import")]
public class Import
{
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
    [XmlAttribute(AttributeName = "operation")]
    public string Operation { get; set; }
    [XmlAttribute(AttributeName = "entity")]
    public string Entity { get; set; }
    [XmlAttribute(AttributeName = "externalReference")]
    public string ExternalReference { get; set; }
    [XmlAttribute(AttributeName = "item")]
    public string Item { get; set; }
    [XmlAttribute(AttributeName = "queryTime")]
    public string QueryTime { get; set; }
    [XmlElement(ElementName = "failureMessage")]
    public string FailureMessage { get; set; }
    [XmlElement(ElementName = "failureDetail")]
    public string FailureDetail { get; set; }
    [XmlElement(ElementName = "duplicateMessage")]
    public string DuplicateMessage { get; set; }
    [XmlElement(ElementName = "duplicateDetail")]
    public string DuplicateDetail { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
    [XmlElement(ElementName = "import")]
    public List<Import> Import { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
    [XmlElement(ElementName = "import")]
    public Import Import { get; set; }
}
[XmlRoot(ElementName = "importDuplicates")]
public class ImportDuplicates
{
    [XmlElement(ElementName = "import")]
    public Import Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class OrderImportResponse
{
    [XmlElement(ElementName = "importSuccesses")]
    public ImportSuccesses ImportSuccesses { get; set; }
    [XmlElement(ElementName = "importFailures")]
    public ImportFailures ImportFailures { get; set; }
    [XmlElement(ElementName = "importDuplicates")]
    public ImportDuplicates ImportDuplicates { get; set; }
}

现在,这对我来说可以了。我可以调用api并取消对OrderImportResponse类的响应的序列化(无需检查它是什么样的xml响应),它将拥有我正在寻找的所有三个场景响应。