My XmlSerializer code is outputting Xml that is much more verbose than I require, how can I control the output settings properly? This is the code:
var stream = new MemoryStream()开发者_StackOverflow社区;
var xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
xmlSerializer.Serialize(stream, objectToSerialize);
string xml = encoding != null ? encoding.GetString(stream.ToArray())
: Encoding.Default.GetString(stream.ToArray());
And the output looks like this:
<?xml version="1.0"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
<Id>1</Id>
<ProductCode>A</ProductCode>
<ProductDescription>Product A</ProductDescription>
<Obsolete xsi:nil="true"></Obsolete>
</Product>
</ArrayOfProduct>
I want to be able to format the Xml like so:
- Remove the xsi:nil="true" from lines with no value
- Collapse empty tags like
<Obsolete></Obsolete>
into the simple form<Obsolete />
When you decorate the class and members using [Serializable] the objects are serialized using the old methods which does not contains xsi:nil="true But when you decorate the class with [DataContract] attribute the XmlSerializer will include the xsi:nil="true.
One way to remove xsi:nil="true is to decorate the class with [Serializable] attribute.
精彩评论