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

时间:2015-12-09 17:09:06

标签: c# xml

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

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

例如;我将有一个像这样的共同响应DTO类:

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

并且ImportOrders方法将有条件地返回响应:

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; }
}

但现在我遇到了问题。成功与成功失败响应包含节点importResult,但具有不同的字段/元素名称。

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

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

1 个答案:

答案 0 :(得分:0)

我相信我很傻......我用错误的方法来解决这个问题。

我做的是;

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

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

所以,我打电话给api而不是发一个订单;我为3个特定场景发送了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响应),它将具有我正在寻找的所有三个场景响应。