I’m working on getting my .NET object serialized / deserialized. As a requirement for our XML files, the object must be inside a master node named mycompany. Here is an example for the file:
<?xml version="1.0" encoding="utf-8"?>
<mycompany>
<station>
<serial>VAA008090067</serial>
</station>
</mycompany>
I’m running into an issue getting this to deserialize. I’m not aware of how to tell the serializer, “Hey, make sure you dig into the mycompany node before you deserialize.”
Here is my current deserializtion code (not accounting for a root node):
Stream binaryStream = File.Open(Filename, FileMode.Open);
XmlSerializer xformatter = xformatter = new XmlSerializer(typeof(T));
obj = (T)xformatter->Deserialize(stream);
I attempted to do the following code: Create an XmlTextStream, read in the file header node, and the mycompany node, then pass the stream to the serializer
Stream binaryStream = File.Open(Filename, FileMode.Open);
xmlReader = gcnew XmlTextReader(binaryStream);
xmlReader.Read(); // add error checking
xmlReader.Read(); // add error checking
xformatter = gcnew XmlSerializer(T.typeid);
obj = (T)xformatter.Deserialize(xmlReader);
The above doesn’t work, throws me an XmlElement error: Root element is missing.
I know there is a simple solution, but I am unable to find it.
Change it to