- Home /
Question by
CoreDLL · Mar 10 at 06:36 PM ·
xmlserializer
XmlSerializer not serializing strings.
[Serializable] public class ServerConfig
{
public string IP;
public ushort PORT;
public ushort WEB_PORT;
public ushort USERS;
public static ServerConfig Load(string Path)
{
if(!File.Exists(Path+".xml")) Save(new ServerConfig(),Path);
using (var Stream = new FileStream(Path + ".xml", FileMode.Open))
{
var xml = new XmlSerializer(typeof(ServerConfig));
return (ServerConfig)xml.Deserialize(Stream);
}
}
public static void Save(ServerConfig Config, string Path)
{
using (var Stream = new FileStream(Path + ".xml", FileMode.Create))
{
var xml = new XmlSerializer(typeof(ServerConfig));
xml.Serialize(Stream,Config);
}
}
}
<?xml version="1.0"?>
<ServerConfig
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PORT>0</PORT>
<WEB_PORT>0</WEB_PORT>
<USERS>0</USERS>
</ServerConfig>
Comment
Best Answer
Answer by asafsitner · Mar 10 at 06:59 PM
Note that a string is an object, and thus nullable, and thus you should initialize it to something (even doing config.IP = "";
will work) in order for XmlSerializer to serialize it, because the XmlSerializer ignores null values by default.
Alternative, you can add a [XmlElement(IsNullable = true)]
attribute to nullable fields to include them in the XML output regardless.
Your answer
Follow this Question
Related Questions
xmlserializer Serializing/deserializing error 1 Answer
XML Error on Load 1 Answer
Best Way to Store Large Number of GameObjects? 1 Answer
How to type Color32 format into XML? 1 Answer
does xml serializer good to store images and 3d models 0 Answers