简体   繁体   English

XmlSerializer。只反序列化特定的类

[英]XmlSerializer.Deserialize only specific classes

This is my situation. 这是我的情况。 I need to create a new application which is based off existing object classes which I'm not allowed to change as its being used by other projects. 我需要创建一个基于现有对象类的新应用程序,由于其他项目正在使用该类,因此我不允许对其进行更改。 But I need to use these object in conjunction with new objects in my new application. 但是我需要在新应用程序中将这些对象与新对象结合使用。 In my new application, each action will have a request and response class, which will be serialized/deserilized as it will be sent over sockets to another company. 在我的新应用程序中,每个动作都将具有一个请求和响应类,由于将通过套接字将其发送给另一家公司,因此将对其进行序列化/反序列化。 I then provide the XSD to the company which they will use to form the response and send back as XML. 然后,我向公司提供XSD,他们将使用它们来形成响应并以XML的形式发送回去。 I then need to deserialize the XML into my newly created object, in this case its called GetAccountDetailsMessageResponse. 然后,我需要将XML反序列化到新创建的对象中,在这种情况下,该对象称为GetAccountDetailsMessageResponse。

[System.Xml.Serialization.XmlRoot("GetAccountDetailsMessageResponse", Namespace = "http://test.com.au/")]
public class GetAccountDetailsMessageResponse 
{ 
            public MyNewHeader Header
    { 
        get; 
        set; 
    } 

    public AccountsResponse Response 
    { 
        get; 
        set; 
    } 
}

//Here is the  existing class structure which I need to use, and cannot

// MAIN CLASS - Containing collection of Accounts
public class AccountsResponse : ResponseHeader
{
    public AccountsResponse()
    {
        Accounts = new List<AccountResponse>();
    }

    public List<AccountResponse> Accounts { get; set; }
}

public class AccountResponse : ResponseHeader
{
    public int AccountNumber { get; set; }

    public string AccountType { get; set; }
}

public class ResponseHeader : MessageHeader
{
    public string ReservedField1 { get; set; }

    public string ReservedField2 { get; set; }

    public string TextMessage { get; set; }

    public string Status { get; set; }

    public TransactStatus TransactStatus { get; set; }
}

public class MessageHeader
{
    public string Function { get; set; }

    public string MessageID { get; set; }

    public int CustomerNumber { get; set; }

    public string AccessCode { get; set; }

    public string SessionID { get; set; }

    public int Flag { get; set; }
}

As you can see here, the class existing class structure is very annoying as most classes derive from Header. 正如您在此处看到的那样,该类现有的类结构非常令人讨厌,因为大多数类都是从Header派生的。 Even the actual entity, AccountResponse, which represents a single account, derives from ResponseHeader. 甚至代表单个帐户的实际实体AccountResponse也从ResponseHeader派生。 Soy you end up with ResponseHeaders everywhere. 大豆使您到处都有ResponseHeaders。

Using the XSD.exe tool from Microsoft, an auto-generated XSD is created which I need to give to other company. 使用Microsoft的XSD.exe工具,创建了自动生成的XSD,需要将其提供给其他公司。

My problem here is, the Original AccountsResponse class derives from ResponseHeader, I already defined my own header property in GetAccountDetailsMessageResponse as Property Header. 我的问题是,原始AccountsResponse类是从ResponseHeader派生的,我已经在GetAccountDetailsMessageResponse中定义了自己的标头属性作为属性标头。

**So here is my question: How do get the XSD tool, or even write a C# method that serializes the object to XML, but IGNORE the ResponseHeader in the AccountsResponse class. **所以这是我的问题:如何获得XSD工具,甚至编写将对象序列化为XML的C#方法,但是在AccountsResponse类中忽略ResponseHeader。 So it should only serialize everything in AccountsResponse, and ignore everything that would be derived from ResponseHeader. 因此,它只应序列化AccountsResponse中的所有内容,而忽略将从ResponseHeader派生的所有内容。

In the end, I want the XSD.exe tool, or a custom C# method, to generate an XSD, that would provide an XML that looks like this: 最后,我希望XSD.exe工具或自定义C#方法生成XSD,该XSD将提供如下所示的XML:

在此处输入图片说明

覆盖AccountsResponse中的所有ResponseHeader / MessageHeader属性,并将XMLIgnore属性附加到它们,或者作为替代,将XmlSchemaProvider属性(定义了传递模式的函数)附加到类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM