简体   繁体   中英

Need help Deserializing XML file to Object C#

I am having issues trying to deserialize an XML file to an object in C#. I am getting the following error on the last line of code:

System.FormatException: 'Input string was not in a correct format.'

When my exception hits and I mouse hover over reader2, it says:

"Whitespace, Value="\\n "

Here is my code sample:

public static void LoadXML(string SIMDURL, string PREMURL)
{

    var MyDownloadedFile = new MemoryStream(new WebClient().DownloadData(SIMDURL));

    XmlSerializer reader = new XmlSerializer(typeof(Simulcast.EntryRaceCard));
    var reader2 = XmlReader.Create(MyDownloadedFile);
    Simulcast.EntryRaceCard overview;
    overview = (Simulcast.EntryRaceCard)reader.Deserialize(reader2);


}

You need a proper class model for the XML. The easiest way to do that is to download copy the whole XML document to your clipboard, and then use Visual Studio's "Paste XML as Classes" functionality. Just create the empty namespace to contain the classes and paste the class definitions into it.

Alternatively you can use the XML Schema Definition Tool (Xsd.exe) to generate classes from the XML doc.

在此处输入图片说明

Then the code you have will work

    class Program
    {
        static WebClient wc = new WebClient();
        public static Simulcast.EntryRaceCard LoadXML(string url)
        {

            var MyDownloadedFile = new MemoryStream(wc.DownloadData(url));

            XmlSerializer reader = new XmlSerializer(typeof(Simulcast.EntryRaceCard));
            var reader2 = XmlReader.Create(MyDownloadedFile);
            var overview = (Simulcast.EntryRaceCard)reader.Deserialize(reader2);

            return overview;


        }
        static void Main(string[] args)
        {
            var url = "https://2m2pperformanceblob.blob.core.windows.net/simdpremcontainer/SIMD20201024KEE_USA.xml";
            var overview = LoadXML(url);
        }
    }

I used the following code to deserialize you URL. I used the xsd.exe to create classes from your schema which is at the top of the xml

--http://ifd.equibase.com/schema/simulcast.xsd

I had to make one property private that isn't used to remove an error

   /// <remarks/>
    private EntryRaceGrade Grade {
        get {
            return this.gradeField;
        }
        set {
            this.gradeField = value;
        }
    }

Here is the code

        const string URL = "https://2m2pperformanceblob.blob.core.windows.net/simdpremcontainer/SIMD20201024KEE_USA.xml";
        static void Main(string[] args)
        {
            XmlReader xReader = XmlReader.Create(URL);
            XmlSerializer serializer = new XmlSerializer(typeof(RaceCard));
            RaceCard raceCard = (RaceCard)serializer.Deserialize(xReader);
        }

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