public class hat { [xmltextattribute] public string name { get; set; } [xmlattribute("color")] public string color { get; set; } } var hat1 = new hat {name="cool hat", color="red"}; var hat2 = new hat {name="funky hat", color=null};
this (notice missing color-attribute on funky hat):
<hats> <hat color="red">cool hat</hat> <hat>funky hat</hat> </hats>
this want:
<hats> <hat color="red">cool hat</hat> <hat color="">funky hat</hat> </hats>
how can force serializer create empty attribute in stead of leaving out?
edit:
turns out idiot , created example contained error, because wanted simplify code example.
if value of color "" (or string.empty), serialized empty attribute. however, had null value, not empty string - hence left out.
so behavior wanted behavior of example created.
sorry, guys!
try using list<hat>
container. using this:
var hats = new list<hat> { new hat { name = "cool hat", color = "red" }, new hat { name = "funky hat", color = string.empty } }; using (var stream = new filestream("test.txt", filemode.truncate)) { var serializer = new xmlserializer(typeof(list<hat>)); serializer.serialize(stream, hats); }
i this:
<?xml version="1.0"?> <arrayofhat xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <hat color="red">cool hat</hat> <hat color="">funky hat</hat> </arrayofhat>
Comments
Post a Comment