我有一个包含我希望用 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
值清空列表中的元素?我真的想避免自定义序列化,但在这种情况下,或许其他一些序列化框架会更好?
答案 0 :(得分:2)
在IsNullable课程中使用XmlArrayItemAttribute = 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
。
答案 1 :(得分:1)
有一个想法 - 您可以使用“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; }
}