Tag: xml

xml serializer: known types with tidy namespace

The KnownType attribute in WCF is XmlInclude in Xml.
These let the serializer know that other types can be encountered which is not know by scanning the contract of the object being serialized. This normally happens when a contract is on a base type, but can hold derived types.

When your xml is visible to the user it matters how clean the text is. Using XmlInclude adds an attribute to let itself know the runtime type like so:
<Item d5p1:type=”Button”.

Now the definition of d5p1 is in the namespace xmlns:d5p1=”http://www.w3.org/2001/XMLSchema-instance”
If this namespace was not already in the root element, the generated xml would have this reference in every item such as:
<Item d5p1:type=”Button” xmlns:d5p1=”http://www.w3.org/2001/XMLSchema-instance” …

Adding this namespace explicitly to the XmlSerializer means that this xmlnd definition will now appear 100000 times in the document annoying the user who is reading it (or worse yet who has to edit it)

If you add code like so, the document magically becomes tidy.

var xml = new XmlSerializer(typeof(T), extraKnownTypes);
var ns = new XmlSerializerNamespaces();
ns.Add("d5p1", "http://www.w3.org/2001/XMLSchema-instance");
xml.Serialize(fileStream, obj, ns);

4: sort xml in vsmdi files before comparing

Discuss

Visual Studio has an annoying way of shuffling up the content of a vsmdi file (the xml file used to store unit tests under lists)

When checking in, its impossible to be sure of the changes made when comparing with the old version.

This tool simply loads the xml, sorts the items inside a list node by name (using Linq.Xml) and saves the file. Read more »