在类级别声明 [XmlElement(IsNullable = true)]

发布于 2025-01-03 16:14:24 字数 504 浏览 0 评论 0原文

有没有办法在类级别声明 [XmlElement(IsNullable = true)] 以便类中的所有属性都将被 XML 序列化,即使它们为 null?

例如

public BankAccount BankAccount { get;放; }

应该导致 ,而不是默认的缺失元素。

我尝试了这个,但编译器(正确地)指出该属性对于类声明无效。

这样做的原因是我不想为所有属性指定这一点。


编辑:这是我正在使用的序列化方法:

        var serializer = new XmlSerializer(FormType);
        var stream = new MemoryStream();

        serializer.Serialize(stream, form);

Is there a way to declare [XmlElement(IsNullable = true)] at class level so that all properties in the class will be XML serialized, even if they are null?

e.g.

public BankAccount BankAccount { get; set; }

Should result in <BankAccount xsi:nil="true" />, rather than the default missing element.

I tried this but the compiler (correctly) states that the attribute is not valid for class declarations.

The reason for this is that I don't want to have to specify this for all properties.


Edit: This is the serialization method I am using:

        var serializer = new XmlSerializer(FormType);
        var stream = new MemoryStream();

        serializer.Serialize(stream, form);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2025-01-10 16:14:24

不幸的是,在为 XML 序列化类注释时必须明确。
每个属性都必须使用其自己的 XmlElement 属性进行注释,除非您想要默认行为。

Unfortunately you have to be explicit when annotating your class for XML serialization.
Each property must be annotated with its own XmlElement attribute unless you want a default behaviour.

゛清羽墨安 2025-01-10 16:14:24

你可以这样做,注意这仅涵盖简单的嵌套类

public class NestedClass
{
    public int MyNestedInt { get; set; }
    public int? MyNestedNullableInt { get; set; }
    public string MyNestedString { get; set; }
}

public class TestOverrideClass
{
    public int MyInt { get; set; }
    public int? MyNullableInt { get; set; }
    public string MyString { get; set; }
    public NestedClass NestedClass { get; set; } = new NestedClass();

    public static string TestOverride()
    {
        string output = TestOverride<TestOverrideClass>(new TestOverrideClass());
        return output;
    }

    public static string TestOverride<T>(object objectToSerialize)
    {
        StringBuilder output = new StringBuilder();

        // Create an XmlWriterSettings object with the correct options.
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = ("\t");
        settings.OmitXmlDeclaration = true;

        // Create the XmlWriter object and write some content.
        XmlWriter writer = null;
        writer = XmlWriter.Create(output, settings);
        var serializer = new XmlSerializer(typeof(T), GetNullOverrides<T>());
        serializer.Serialize(writer, objectToSerialize);

        return output.ToString();
    }

    private static XmlAttributeOverrides GetNullOverrides<T>()
    {
        // Create an XmlAttributes object with IsNullable set to true
        XmlAttributes attributes = new XmlAttributes();
        XmlElementAttribute attribute = new XmlElementAttribute { IsNullable = true };
        attributes.XmlElements.Add(attribute);

        // Create an XmlAttributeOverrides object
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();

        AddOverRides(typeof(T), attributes, overrides);

        return overrides;
    }

    private static XmlAttributeOverrides AddOverRides(Type type, XmlAttributes attributes, XmlAttributeOverrides overrides)
    {
        // Apply the XmlAttributes to all properties T
        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            if (!property.PropertyType.IsValueType)
                overrides.Add(type, property.Name, attributes);
            if (property.PropertyType.IsClass)
                AddOverRides(property.PropertyType, attributes, overrides);
        }
        return overrides;
    }
}

You could do it like this, note this only covers simple nested classes

public class NestedClass
{
    public int MyNestedInt { get; set; }
    public int? MyNestedNullableInt { get; set; }
    public string MyNestedString { get; set; }
}

public class TestOverrideClass
{
    public int MyInt { get; set; }
    public int? MyNullableInt { get; set; }
    public string MyString { get; set; }
    public NestedClass NestedClass { get; set; } = new NestedClass();

    public static string TestOverride()
    {
        string output = TestOverride<TestOverrideClass>(new TestOverrideClass());
        return output;
    }

    public static string TestOverride<T>(object objectToSerialize)
    {
        StringBuilder output = new StringBuilder();

        // Create an XmlWriterSettings object with the correct options.
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = ("\t");
        settings.OmitXmlDeclaration = true;

        // Create the XmlWriter object and write some content.
        XmlWriter writer = null;
        writer = XmlWriter.Create(output, settings);
        var serializer = new XmlSerializer(typeof(T), GetNullOverrides<T>());
        serializer.Serialize(writer, objectToSerialize);

        return output.ToString();
    }

    private static XmlAttributeOverrides GetNullOverrides<T>()
    {
        // Create an XmlAttributes object with IsNullable set to true
        XmlAttributes attributes = new XmlAttributes();
        XmlElementAttribute attribute = new XmlElementAttribute { IsNullable = true };
        attributes.XmlElements.Add(attribute);

        // Create an XmlAttributeOverrides object
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();

        AddOverRides(typeof(T), attributes, overrides);

        return overrides;
    }

    private static XmlAttributeOverrides AddOverRides(Type type, XmlAttributes attributes, XmlAttributeOverrides overrides)
    {
        // Apply the XmlAttributes to all properties T
        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            if (!property.PropertyType.IsValueType)
                overrides.Add(type, property.Name, attributes);
            if (property.PropertyType.IsClass)
                AddOverRides(property.PropertyType, attributes, overrides);
        }
        return overrides;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文