c# - Why XmlSerializer adding garbage value to end tag of the root element? -
i trying write value xml values using xmlserializer , below code. code called on selection change of dropdown list. first time works fine second selection change event inserts garbage value xml shown below.
protected void updatedefaultreasonconfig(object sender, eventargs e) { string configfile = configurationsettings.appsettings["defaultoutagereasonconfig"]; defaultoutagereason defaultoutagereason = new defaultoutagereason(); defaultoutagereason.outagereason = this._defaultoutagereason.text; xmlserializer writer = new xmlserializer(typeof(defaultoutagereason)); using (filestream write = file.openwrite(configfile)) { writer.serialize(write, defaultoutagereason); } }
and resulting xml is:
<?xml version="1.0"?> <defaultoutagereason xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <outagereason>storm damage</outagereason> </defaultoutagereason>eason>
so please me why adding value end tag of root.
you need file.create
instead of file.openwrite
because overwrites old one, , if new file shorter see garbage.
Comments
Post a Comment