简体   繁体   中英

XML Deserialization {“Root element is missing.”} Error

I'm adding a new project to my Solution, and have added the code posted below. The LoadXML() method, variable setup, and file deserialization is done exactly as I have done it several other times throughout this Solution, but suddenly now for this part it is throwing this annoying "{"Root element is missing."}" error - even when there is no XML present or when I use a different XML I know works. This means to me that the bug is obviously in the code, but since the code is almost carbon-copied from my previous file transfers, I don't know how that could be either. The other situations for this error previously posted on this website do not really conform to my situation, so I started a fresh one in hopes I might land closer to an answer!

    //fileName and XML variables for serialization/deserialization
    const string fileName = "SimulatedTrain1.xml";
    XmlSerializer xml = new XmlSerializer(typeof(BindingList<SimulatedTrain>));

    //Create BindingList object to hold XML data
    public BindingList<SimulatedTrain> SimulatedTrain = new BindingList<SimulatedTrain>();

    public void LoadXML()
    {
        try
        {
            using (var fs = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                var tempTrain = (BindingList<SimulatedTrain>)xml.Deserialize(fs);
                SimulatedTrain = tempTrain;
            }                
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message, ex);
        }
    }

The LoadXML() gets called on the form_load

Edit: Here's a part of my XML file (the rest is basically repeat data)

在此处输入图片说明

First, make sure that the XML file is copied into your build directory by selecting "Copy to Output Directory" to be "Copy always" .

Assuming that the file exists, you probably have a type conversion error in deserializing the XML file.

The following works:

SimulatedTrain.cs:

[XmlRoot("ArrayOfSimulatedRailCar")]
public class SimulatedTrain
{
    [XmlElement("SimulatedRailCar")]
    public List<SimulatedRailCar> Cars { get; set; }
}

SimulatedRailCar.cs:

public class SimulatedRailCar
{
    string Name { get; set; }
    // More properties here...
}

TrainProg.cs:

const string fileName = "SimulatedTrain1.xml";
XmlSerializer xml = new XmlSerializer(typeof(SimulatedTrain));

public SimulatedTrain SimulatedTrain;

public void LoadXML()
{
    Contract.Assert(File.Exists(fileName));

    try
    {
        using (var fs = new FileStream(fileName, FileMode.Open))
        {
            SimulatedTrain = (SimulatedTrain)xml.Deserialize(fs);
        }                
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message, ex);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM