我有一个类,其中包含一个要使用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
为null时,它会序列化为空字符串:
<property name="foo" />
而不是null。我希望这个值要么被完全省略,要么看起来像这样:
<property name="foo" xsi:nil="true" />
是否可以根据列表中的XmlText
值将元素置空?我确实试图避免自定义序列化,但在这种情况下,也许其他序列化框架会更好?
在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
。
有一个想法-您可以使用"NameSpecified",但在get中检查"value"的值。也就是说,如果Value为null,那么Name也不会被输出。
不幸的是,您仍然会有一个null属性xml元素tho;我希望这更容易被接受。。。
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; }
}