在嵌套属性上使用XmlAttributeOverrides

时间:2013-05-13 15:35:38

标签: c# class xmlserializer xml-attribute xmlignore

我正在尝试使用XmlAttributeOverrides来控制在序列化类之后哪些类属性出现在xml中。它适用于“根”类上的属性,但不适用于嵌套属性。这是一个简单的例子来说明我想要完成的任务。

我的班级层次结构如下:

public class Main
{
    public string Name { get; set; }
    public Location Address { get; set; }
}

public class Location
{
    public string StreetAddress { get; set; }
    public Contact ContactInfo{ get; set; }
}

public class Contact
{
    public string PhoneNumber { get; set; }
    public string EmailAddr { get; set; }
}

当我序列化Main()时,我得到这样的东西:

<Main>
    <Name></Name>
    <Address>
        <StreetAddress></StreetAddress>
        <ContactInfo>
            <PhoneNumber></PhoneNumber>
            <EmailAddr></EmailAddr>
        </ContactInfo>
    </Address>
</Main>

我能做的是使用以下名称或地址保持显示:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = true;
attribs.XmlElements.Add(new XmlElementAttribute("Address"));
overrides.Add(typeof(Main), "Address", attribs);
xs = new XmlSerializer(typeof(Main), overrides);

我还需要做的是保持Main.Address.ContactInfo被序列化 SOMETIMES (如果它是空的)。我尝试了以下但是它们不起作用:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = true;
attribs.XmlElements.Add(new XmlElementAttribute("ContactInfo "));
overrides.Add(typeof(Contact), "ContactInfo ", attribs);
xs = new XmlSerializer(typeof(Contact), overrides);

和...

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = true;
attribs.XmlElements.Add(new XmlElementAttribute("ContactInfo "));
overrides.Add(typeof(Main.Address.ContactInfo), "ContactInfo ", attribs);
xs = new XmlSerializer(typeof(Main.Address.ContactInfo), overrides);

我实际上已经尝试了很多,包括使用XPath语句将属性名称指定为目标,但不希望尝试失败填充此页面。我用这种方法甚至可以问这个问题吗?

3 个答案:

答案 0 :(得分:4)

有更简单的方法来实现您的目标。

您说如果/Main/Address/ContactInfo不包含任何数据,那么您要实现的目标是不序列化ContactInfo

如果您按原样保留代码,它将序列化所有Main的属性,无论它们是null还是空。第一步,您需要向所有对象添加XmlSerializerNamespaces属性,否则每个空对象将被序列化为<myElement xsi:nil="true" />。这可以很容易地完成,如下所示:

public MyXmlElement
{
    public MyXmlElement()
    {
        // Add your own default namespace to your type to prevet xsi:* and xsd:*
        // attributes from being generated.
        this._namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] {
            new XmlQualifiedName(string.Empty, "urn:myDefaultNamespace") });
    }

    [XmlElement("MyNullableProperty", IsNullable=false)]
    public string MyNullableProperty
    {
        get
        {
            return string.IsNullOrWhiteSpace(this._myNullableProperty) ? 
                null : this._myNullableProperty;
        }
        set { this._myNullableProperty = value; }
    }

    [XmlNamespacesDeclaration]
    public XmlSerializerNamespaces Namespaces { get { return this._namespaces; } }
    private XmlSerializerNamespaces _namespaces;
}

上面的代码声明了一个Namespaces属性,该属性包含XML对象的所有相关命名空间。您应该为所有对象提供默认命名空间(以上面的代码为模型)。这可以防止在序列化对象时输出xsi:*xsd:*属性。另外,使用System.Xml.Serialization.XmlElementAttribute指定元素不可为空。

此外,通过检查string.IsNullOrWhiteSpace(someVariable)并返回null,然后是 完成上述操作后,属性不会被序列化。

所以,将这些全部放在一起为你的Location课程:

public class Location
{
    // You should have a public default constructor on all types for (de)sereialization.
    public Location()
    {
        this._namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] {
            new XmlQualifiedName(string.Empty, "urn:myNamespace"); // Default namespace -- prevents xsi:nil="true" from being generated, as well as xsd:* attributes.
        });
    }

    public string StreetAddress
    {
        // If you don't want <StreetAddress xsi:nil="true" /> to be generated, do this:
        get { return string.IsNullOrEmpty(this._streetAddress) ? null : this._streetAddress; }

        // Otherwise, if you don't care, just do
        // get;

        // Only need to implement setter if you don't want xsi:nil="true" to be generated.
        set { this._streetAddress = value; }

        // Otherwise, just
        // set;
    }
    private string _streetAddress;

    [XmlElement("ContactInfo", IsNullable=false)]
    public Contact ContactInfo
    {
        // You must definitely do this to prevent the output of ContactInfo element
        // when it's null (i.e. contains no data)
        get
        {
            if (this._contactInfo != null && string.IsNullOrWhiteSpace(this._contactInfo.PhoneNumber) && string.IsNullOrWhiteSpace(this._contactInfo.EmailAddr))
                return null;

             return this._contactInfo;
        }

        set { this._contactInfo = value; }
    }
    private Contact _contactInfo;

    [XmlNamespacesDeclarations]
    public XmlSerializerNamespaces Namespaces
    {
        get { return this._namespaces; }
    }
    private XmlSerializerNamespaces _namespaces;
}

Location类进行这些更改后,如果没有属性为null,空或空格,或者ContactInfo,则不应再将空ContactInfo属性序列化为XML本身是空的。

您应该对其他对象进行类似的更改。

有关.NET XML序列化的更多信息,请参阅我的其他stackoverflow答案:

答案 1 :(得分:4)

对于其他试图使用XmlAttributeOverrides执行此操作的人,事实证明@ user1437872非常接近找到答案。以下是忽略嵌套元素ContactInfo的覆盖代码。

$words=include('array.php');

答案 2 :(得分:0)

无需向属性添加ContactInfo元素

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = true;
overrides.Add(typeof(Address), "ContactInfo ", attribs);
xs = new XmlSerializer(typeof(Main), overrides);
相关问题