7

我有一个包含我希望用XmlSerializer序列化的数组的类:

[XmlArray("properties")]
[XmlArrayItem("property", IsNullable = true)]
public List<Property> Properties { get; set; }

Property是一个包含一个属性和一些的类XmlText

[XmlAttribute("name")]
public string Name { get; set; }

[XmlText]
public string Value { get; set; }

问题是当Value为空时,它序列化为一个空字符串:

<property name="foo" />

而不是null。我正在寻找要完全省略的值,或者看起来像这样:

<property name="foo" xsi:nil="true" />

是否可以根据其XmlText值将列表中的元素清空?我真的想避免自定义序列化,但在这种情况下,也许其他一些序列化框架会更好?

4

2 回答 2

2

在XmlArrayItemAttribute类中使用IsNullable =true 。举个例子。

[XmlRoot("Root")]
public class Root
{
    [XmlArrayItem("Element", IsNullable = true)]
    public string[] Elements { get; set; }
}

Visual Studion 2012 和 .Net 4.5 中的一些示例代码:

using System.Xml.Serialization;

...

// Test object
Root root;
root = new Root();
root.Elements = new string[] { null, "abc" };

using(MemoryStream stream = new MemoryStream())
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Root));
    xmlSerializer.Serialize(stream, root);

    Console.WriteLine(new string(Encoding.UTF8.GetChars(stream.GetBuffer())));
}

输出是(为清楚起见添加了换行符):

<?xml version="1.0"?>
<Root 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Element>
    <string xsi:nil="true" />
    <string>abc</string>
  </Element>
</Root>

并且具有复杂类型(也在 Visual Studio 2012 上的 .Net 4.5 中):

    public class MyProperty
    {
        public string Foo { get; set; }
    }

    [XmlRoot("Root")]
    public class Root
    {
        [XmlArrayItem("Element", IsNullable = true)]
        public MyProperty[] Elements { get; set; }
    }

    ,,,

    Root root;
    root = new Root();
    root.Elements = new MyProperty[] { null, new MyProperty{ Foo = "bar" } };

    // Other code is as above

使用上面相同的代码会产生:

<?xml version="1.0"?>
<Root 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Elements>
    <Element xsi:nil="true" />
    <Element>
      <Foo>bar</Foo>
    </Element>
  </Elements>
</Root>

还要记住,类型必须是引用类型(例如,不是结构)才能写出xsi:nil=true

于 2012-09-08T07:08:40.847 回答
1

一个想法 - 您可以使用“NameSpecified”,但在 get 中检查“Value”的值。意思是,如果 Value 为 null,则 Name 也不会输出。

不幸的是,您仍然会有一个 null 属性 xml 元素;我希望这更能被接受...

class Program
{
    static void Main(string[] args)
    {
        ObjectWithProperties obj = new ObjectWithProperties()
        {
            Properties = new List<Property>()
        };

        Property p = new Property();
        p.Name = "This WILL Show Up";
        p.Value = "I'm here";
        obj.Properties.Add(p);

        Property p1 = new Property();
        p1.Name = "This Will NOT Show Up";
        obj.Properties.Add(p1);

        Console.WriteLine(ToXmlString(obj));
        Console.ReadLine();
    }

    public static string ToXmlString(object value)
    {
        if (value == null) return string.Empty;
        XmlSerializer ser = new XmlSerializer(value.GetType());
        MemoryStream ms = new MemoryStream();
        ser.Serialize(ms, value);
        return Encoding.UTF8.GetString(ms.ToArray());
    }

}
public class ObjectWithProperties
{
    [XmlArray("properties")]
    [XmlArrayItem("property", IsNullable = true)]
    public List<Property> Properties { get; set; }
}

public class Property
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlIgnore]
    public bool NameSpecified
    {
        get { return !string.IsNullOrEmpty(Value); }
    }

    [XmlText]
    public string Value { get; set; }

}
于 2012-09-08T04:27:18.923 回答