Reputation: 121
I have a list full of building objects and i want so serialize it to an XML file. Basic serialization is working fine, yet turning the values into attributes for better readability failes strangely on all but the int-values.
Here is the building class i am using that should be serialized:
public class building
{
[XmlAttribute]
public string strName { set; get; } //name
[XmlAttribute]
public int intCount { set; get; } //count of building
[XmlAttribute]
public int intCost { set; get; } //running cost of building
[XmlAttribute]
public string strProd { set; get; } //product
[XmlAttribute)]
public string strRes1 { set; get; } //first ressource required
[XmlAttribute]
public string strRes2 { set; get; } //second ressource required
[XmlAttribute]
public int intTime { set; get; } //time to build in seconds
}
the XML-file outputtet, if i leave out the [XMLAttribute] tags, is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBuilding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<building>
<strName>Holzfäller</strName>
<strKind>Produktion</strKind>
<intCost>2</intCost>
<strProd>Holz</strProd>
<strRes1 />
<strRes2 />
<intTime>105</intTime>
</building>
Wich as is said is fine, just hard get an overview with well over 50 buildings in the file. The XML-File created with the above code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBuilding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<building intCount="0" intCost="0" intTime="0" />
<building intCount="0" intCost="0" intTime="0" />
As you can see only the int values are added and even those are FALSE. All values are set to zero. Strangely enough if i update one value in my programm after creating the list, the change is reflected in the XML as well. Just the base values seem to be ignored / changed to zero.
Edit: i also tried setting the DataType like this:
[XmlAttribute(DataType = "string")]
But this also just makes the string go poof and not show up at all.
Kinda lost on this one. If i just leave the Attribute stuff out all is working fine, so i am at least sure the rest of my code is not causing any issues. But why would XMLSerializer ignore object attributes that are strings and reset all int values? How can i change this?
Sorry if this has been asked elsewhere, i fail to find the right words for google to solve my issue. Mainly because i don't understand whats happening here and don't know where to start. Documentations i find simply show what i am doing, claiming that it will work. But it does not obviously :-(
Upvotes: 2
Views: 1240
Reputation: 14332
Your string
s are null so they are not written with the serialization.
Initialize your string fields to string.Empty
in building
's default constructor and they will appear in the output.
I tested it with this constructor in building
:
public building()
{
strProd = "";
}
Output:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfBuilding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<building intCount="0" intCost="0" strProd="" intTime="0" />
<building intCount="0" intCost="0" strProd="" intTime="0" />
</ArrayOfBuilding>
Why is the behavior different in attributes?
Xml is capable of displaying nullable types in elements by omitting the content. Attributes however, can not display nullables and differentiate from an empty string. Therefore, the attribute is omitted entirely.
Upvotes: 2