Reputation: 55
I am trying to deserialize an XML file. I wrote a class structure of the file. When I trying to deserialize I am getting this error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in XML document (0, 0).
I do not know what is wrong with the class structure:
Here is the XML:
<?xml version="1.0" encoding="utf-8" ?>
<Commands>
<Command Name="ACK" Value="00"></Command>
<Command Name="Supervise / Poll" Value="01"></Command>
<Command Name="Sequence number re-sync" Value="02"></Command>
<Command Name="STATUS" Value="10"></Command>
<Command Name="REQUEST" Value="20"></Command>
<Command Name="RESPONSE" Value="30"></Command>
<Command Name="Validation" Value="33"></Command>
<Command Name="Stop Request" Value="35"></Command>
<Command Name="WRITE" Value="40"></Command>
<Command Name="DATA" Value="50"></Command>
<Command Name="CONFIG" Value="60"></Command>
<Command Name="STARTUP" Value="70"></Command>
<Command Name="Pass-through" Value="80"></Command>
<Command Name="MULTI COMMAND" Value="90"></Command>
</Commands>
Here is the class structure:
[XmlRoot("Commands")]
public class XMLCommands
{
[XmlElement("Command")]
public List<XMLCommand> Command{ get; set; }
public XMLCommands()
{
Command = new List<XMLCommand>();
}
public class XMLCommand
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("Value")]
public string Value { get; set; }
public XMLCommand()
{
Name = "";
Value = "";
}
}
}
Here is the method I read the XML with:
public CoreBusAnalyzing(string PathAnalyze)
{
_pathToAnalyze = PathAnalyze;
_stopAnalyzing = false;
_XMLModule = new XMLModule();
//XmlSerializer serializer = new XmlSerializer(typeof(XMLModule));
//StreamReader reader = new StreamReader("XMLModule.xml");
//string tmp = reader.ReadToEnd();
//_XMLModule = (XMLModule)serializer.Deserialize(reader);
//reader.Close();
_XMLCommands = new XMLCommands();
XmlSerializer serializer = new XmlSerializer(typeof(XMLCommands));
StreamReader reader = new StreamReader("XMLCommand.xml");
reader.ReadToEnd();
_XMLCommands = (XMLCommands)serializer.Deserialize(reader);
reader.Close();
}
Upvotes: 0
Views: 66
Reputation: 22001
You are reading your data stream to the end, then passing the stream to the Deserialize
method - at this point the stream has already been read.
You should remove the reader.ReadToEnd()
line from your code.
Upvotes: 3