基于DeserializeXml结果字段

时间:2017-11-22 14:29:32

标签: c# xml-deserialization

我有样本课

[XmlType(TypeName = "Profiles")]
public class Profiles : List<ProfileSearch> { }

[XmlType(TypeName = "Profile")]
public class ProfileSearch {
    public int ProfileId { get; set; }
    public string Company { get; set; }
    public AdditionalData AdditionalData { get; set; }
}

public class AdditionalData {
    public int UniqueId { get; set; }
    public string Email { get; set; }
    public string LocalNumber { get; set; }
    public string PrimaryTitle { get; set; }
}

我使用下面的代码反序列化XML

var result = Common.Utilities.Serialization.DeserializeXml<Profiles>(XMLData);

这里XMLData是XML格式的数据,由不同的客户提供。

有时,XMLData包含新字段,这些字段未在AdditionalData类中定义为属性,并且这些字段可能会根据客户端进行更改,因此我无法将这些字段添加为类中的属性

反序列化XMLData后,缺少这些新字段数据。 所以,我想基于XMLData动态地将这些新字段添加到类作为属性,这样我就不会错过要显示的字段数据。

下面的

是XMLData值示例

Data1

<Profiles>
<Profile>
    <ProfileId>1</ProfileId>
    <Company>Company1</Company>
    <AdditionalData>
        <UniqueId>12455</UniqueId>
        <Email>sample1@abc.com</Email>
        <LocalNumber>1123455678</LocalNumber>
        <Title>Sample Title1</Title>
        <Fr_Feminine>data1</Fr_Feminine>
        <Fr_Masculine>data2</Fr_Masculine>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>2</ProfileId>
    <Company>Company2</Company>
    <AdditionalData>
        <UniqueId>12435</UniqueId>
        <Email>sample2@abc.com</Email>
        <LocalNumber>435454454</LocalNumber>
        <Title>Sample Title2</Title>
        <Fr_Feminine>data21</Fr_Feminine>
        <Fr_Masculine>data22</Fr_Masculine>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>3</ProfileId>
    <Company>Company3</Company>
    <AdditionalData>
        <UniqueId>23421</UniqueId>
        <Email>sample3@abc.com</Email>
        <LocalNumber>343554545</LocalNumber>
        <Title>Sample Title3</Title>
        <Fr_Feminine>data31</Fr_Feminine>
        <Fr_Masculine>data32</Fr_Masculine>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>4</ProfileId>
    <Company>Company4</Company>
    <AdditionalData>
        <UniqueId>34252</UniqueId>
        <Email>sample4@abc.com</Email>
        <LocalNumber>233434535</LocalNumber>
        <Title>Sample Title4</Title>
        <Fr_Feminine>data41</Fr_Feminine>
        <Fr_Masculine>data42</Fr_Masculine>
    </AdditionalData>
</Profile>

数据2

<Profiles>
<Profile>
    <ProfileId>1</ProfileId>
    <Company>Company1</Company>
    <AdditionalData>
        <UniqueId>12455</UniqueId>
        <Email>sample1@abc.com</Email>
        <LocalNumber>1123455678</LocalNumber>
        <Title>Sample Title1</Title>
        <Designation>Designation1</Designation>
        <Address>Address1</Address>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>2</ProfileId>
    <Company>Company2</Company>
    <AdditionalData>
        <UniqueId>12435</UniqueId>
        <Email>sample2@abc.com</Email>
        <LocalNumber>435454454</LocalNumber>
        <Title>Sample Title2</Title>
        <Designation>Designation2</Designation>
        <Address>Address2</Address>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>3</ProfileId>
    <Company>Company3</Company>
    <AdditionalData>
        <UniqueId>23421</UniqueId>
        <Email>sample3@abc.com</Email>
        <LocalNumber>343554545</LocalNumber>
        <Title>Sample Title3</Title>
        <Designation>Designation1</Designation>
        <Address>Address3</Address>
    </AdditionalData>
</Profile>
<Profile>
    <ProfileId>4</ProfileId>
    <Company>Company4</Company>
    <AdditionalData>
        <UniqueId>34252</UniqueId>
        <Email>sample4@abc.com</Email>
        <LocalNumber>233434535</LocalNumber>
        <Title>Sample Title4</Title>
        <Designation>Designation2</Designation>
        <Address>Address4</Address>
    </AdditionalData>
</Profile>

已添加配置文件结束标记但未显示,因此请在使用此XML数据时为配置文件添加结束标记

1 个答案:

答案 0 :(得分:0)

使用动态解决方案,如:

[XmlType(TypeName = "Profile")]
public class ProfileSearch {
    public int ProfileId { get; set; }
    public string Company { get; set; }
    public List<Field> AdditionalData { get; set; }
}

public class Field {
    public string FieldName { get; set; }
    public object Value { get; set; }

    //More Properties Like DataType...
}

当然,使用标准XML解析器解析起来并不容易,但是为了动态创建属性,你无论如何都要解析XML ...