.net 不同对象数组的XML序列化

xmd2e60i  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(118)

以前,我使用了一个外部服务,并从他们那里收到了类似于以下XML的响应:

<ns0:response xmlns:ns0='http://ws.gp.mnpartners.kz/services/bvu'>
    <data>
        <item>
            <person>
                <request>
                    <number>860517450246</number>
                    <surname>Foo</surname>
                    <name>Bar</name>
                    <birthDate>1986-05-17</birthDate>
                </request>
            </person>
        </item>
        <item>
            <comapany>
                <request>
                    <companyId>010940000162</companyId>
                    <fullName>SomeCompanyName</fullName>
                </request>
            </comapany>
        </item>
    </data>
</ns0:response>

反序列化非常简单,有一个包含数据类型数组的Response类,数据类型由person和company字段组成。由于每个元素都包含company和person类型,因此反序列化成功。
我的班级示例:

public class Response
{
    public Data[] dataField;

    [System.Xml.Serialization.XmlArrayItem("item", IsNullable = false)]
    public Data[] data
    {
        get
        {
            return this.DataField;
        }
        set
        {
            this.DataField = value;
        }
    }
}

public class Data
{
    public Person personFiled;
    public Company companyFiled;

    public Person person
    {
        get
        {
            return this.personFiled;
        }
        set
        {
            this.personFiled = value;
        }
    }

    public Company company
    {
        get
        {
            return this.companyFiled;
        }
        set
        {
            this.companyFiled = value;
        }
    }
}

public class Person
{
    /// some properties
}

public class Company
{
    /// some properties
}

此时,外部服务已经更改了XML响应的结构,现在看起来像这样:

<ns0:response xmlns:ns0='http://ws.gp.mnpartners.kz/services/bvu'>
<data>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:person">
        <request>
            <number>860517450246</number>
            <surname>Foo</surname>
            <name>Bar</name>
            <birthDate>1986-05-17</birthDate>
        </request>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:comapany">
        <request>
            <companyId>010940000162</companyId>
            <fullName>SomeCompanyName</fullName>
        </request>
    </item>
</data>

我想在反序列化后得到一个就绪的对象,即使person和company对象在不同的数组中。
请提出反序列化此XML的最佳方法,任何想法。

tcbh2hod

tcbh2hod1#

Xml在20多年前就很流行了,我想现在是时候尝试一些新技术了,比如Newtonsoft.json

using Newtonsoft.Json;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    JArray item = (JArray) JObject.Parse(JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.Indented, true)
                                   .Replace("ns0:", "")
                                   .Replace("@xsi:", ""))["item"];

    Company company = item[1]["request"].ToObject<Company>();
    Person persony = item[0]["request"].ToObject<Person>();

你只需要Person和Company类
如果你不确定人和公司在xml中的顺序是否总是相同的,试试Linq

Company company=item.Where(x=> (string) x["type"]=="comapany")
                       .Select(x=> x["request"].ToObject<Company>())
                       .FirstOrDefault();
                       
    Person persony=item.Where(x=> (string) x["type"]=="person")
                       .Select(x=> x["request"].ToObject<Person>())
                       .FirstOrDefault();

相关问题