C# , Why is using TextReader throwing exception after Visual Studio update?
Why is Deserialize
throwing an exception after I updated Visual Studio 2010?
EDIT (Question change):
I got it to work by removing theusing
statement and calling Dispose
manually on TextReader gTr
.
New question:
Why does theusing
statement cause errors while reading with TextReader
(after the update)?
I'm baffled by this. All I did was update Visual Studio and it no longer works. It worked perfectly fine before (for weeks). It also matches many many examples I've read. I don't see how any of it is wrong, or how Root
is missing, or how there's an error in XML document (0, 0)
.
//EXCEPTION
System.InvalidOperationException was caught
Message=There is an error in XML document (0, 0).
Source=System.Xml
InnerException: System.Xml.XmlException
Message=Root element is missing.
LineNumber=0
LinePosition=0
//SERIALIZE
SGlobalSettings gSettings = new SGlobalSettings();
XmlSerializer gXmls = new XmlSerializer(typeof(SGloba开发者_Go百科lSettings));
using (TextWriter gTw = new StreamWriter("global.xml"))
{
gXmls.Serialize(gTw, gSettings);
}
//DESERIALIZE
if (File.Exists("global.xml"))
{
SGlobalSettings global;
XmlSerializer gXmls = new XmlSerializer(typeof (SGlobalSettings));
using (TextReader gTr = new StreamReader("global.xml"))
{
global = (SGlobalSettings)gXmls.Deserialize(gTr);
}
}
//OBJECT
[XmlRootAttribute("Global")]
public class SGlobalSettings
{
public string key { get; set; }
public string last { get; set; }
public SGlobalSettings() { }
}
//XML
<?xml version="1.0" encoding="utf-8" ?>
<Global xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<key>KEY</key>
<last>LAST</last>
</Global>
Thanks for any help!!!
I've had the exact same issue before. My best guess is that the "root element" the XmlSerializer is looking for is an element with the same name as the type it's trying to deserialize. So, changing your XML to the following should work: (the changes are in bold)
<?xml version="1.0" encoding="utf-8" ?>
<SGlobalSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<key>KEY</key>
<last>LAST</last>
</SGlobalSettings>
I don't know why the TextReader would be reading a file incorrectly when automatically disposed, but are you aware that there's an overload of XmlSerializer.Deserialize
that accepts a Stream
instance as an argument? Using this overload might solve your issue; multiple overloads might exist because each one of them uses the underlying stream differently. This seems in sync with Microsoft's typical, cryptically-hidden hooks.
I'd try bypassing the TextReader altogether and using the following code instead:
XmlSerializer gXmls = new XmlSerializer(typeof (SGlobalSettings));
using (Stream gStream = File.OpenRead("global.xml"))
{
global = (SGlobalSettings)gXmls.Deserialize(gStream);
}
Error at (0, 0) usually means that your code can open the file but the file is empty. Try use Flush() method in serialize section.
Also, your code works fine on my visual studio 2010 SP1;
精彩评论