views:

33

answers:

1

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 serialize." 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 a XmlTextStream, read in the file header node, and the company 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 a "XmlElement Error: Root Element is missing"

I know there is a simple solution, but I am unable to find it.

Thanks in advance

+2  A: 

Change it to

xformatter.Deserialize(xmlReader.ReadSubTree());
SLaks
The following exception occured: ReadSubTree can only be called if the Reader is on an element node. I also tried throwing a single writer->Read() before the deserialization. Is the way I'm assigning my read from a stream incorrect?
greggorob64
Replace your `Read()` calls with a single call to `ReadStartElement()`
SLaks
You, sir, are a scholar and a saint.
greggorob64