4

I'm trying to deserialize an XML file which I receive from a vendor with XmlSerializer, however im getting this exception: There is an error in XML document (1, 2).InnerException Message "<delayedquotes xmlns=''> was not expected.. I've searched the stackoverflow forum, google and implemented the advice, however I'm still getting the same error. Please find the enclosed some content of the xml file:

<delayedquotes id="TestData">
  <headings>
    <title/>
    <bid>bid</bid>
    <offer>offer</offer>
    <trade>trade</trade>
    <close>close</close>
    <b_time>b_time</b_time>
    <o_time>o_time</o_time>
    <time>time</time>
    <hi.lo>hi.lo</hi.lo>
    <perc>perc</perc>
    <spot>spot</spot>
  </headings>
  <instrument id="Test1">
    <title id="Test1">Test1</title>
    <bid>0</bid>
    <offer>0</offer>
    <trade>0</trade>
    <close>0</close>
    <b_time>11:59:00</b_time>
    <o_time>11:59:00</o_time>
    <time>11:59:00</time>
    <perc>0%</perc>
    <spot>0</spot>
  </instrument>
</delayedquotes>

and the code

[Serializable, XmlRoot("delayedquotes"), XmlType("delayedquotes")]
public class delayedquotes
{
    [XmlElement("instrument")]
    public string instrument { get; set; }
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("bid")]
    public double bid { get; set; }
    [XmlElement("spot")]
    public double spot { get; set; }
    [XmlElement("close")]
    public double close { get; set; }
    [XmlElement("b_time")]
    public DateTime b_time { get; set; }
    [XmlElement("o_time")]
    public DateTime o_time { get; set; }
    [XmlElement("time")]
    public DateTime time { get; set; }
    [XmlElement("hi")]
    public string hi { get; set; }
    [XmlElement("lo")]
    public string lo { get; set; }
    [XmlElement("offer")]
    public double offer { get; set; }
    [XmlElement("trade")]
    public double trade { get; set; }

    public delayedquotes()
    {

    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Lindsay Fisher
  • 43
  • 1
  • 1
  • 6

3 Answers3

4

Maybe you can try this code:

[Serializable, XmlRoot("delayedquotes"), XmlType("delayedquotes")]
public class DelayedQuotes 
{
    public DelayedQuotes()
    {
        instrument = new List<Instrument>();
    }

    [XmlElement("instrument")]
    public List<Instrument> instrument { get; set; }
}

[XmlType("instrument")]
public class Instrument
{
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("bid")]
    public double bid { get; set; }
    [XmlElement("spot")]
    public double spot { get; set; }
    [XmlElement("close")]
    public double close { get; set; }
    [XmlElement("b_time")]
    public DateTime b_time { get; set; }
    [XmlElement("o_time")]
    public DateTime o_time { get; set; }
    [XmlElement("time")]
    public DateTime time { get; set; }
    [XmlElement("hi")]
    public string hi { get; set; }
    [XmlElement("lo")]
    public string lo { get; set; }
    [XmlElement("offer")]
    public double offer { get; set; }
    [XmlElement("trade")]
    public double trade { get; set; }
}

Also, here is a sample code to test the classes above:

static void Main(string[] args)
{
    Console.WriteLine("Initiating test!");

    XmlSerializer serializer = new XmlSerializer(typeof(DelayedQuotes));
    FileStream xmlFile = new FileStream("testXml.xml", FileMode.Open);

    DelayedQuotes quotes = (DelayedQuotes) serializer.Deserialize(xmlFile);

    Console.WriteLine("Finalizing test!");
}
Felipe
  • 6,312
  • 11
  • 52
  • 70
  • Hello Komyg, just got back into the office. thanks for the feedback will test the code. – Lindsay Fisher Feb 28 '12 at 13:43
  • Komyg, your also work well, however only one record is available after I serialised the xml file. How do I get the rest of the records or serialised the xml into a collection. – Lindsay Fisher Feb 28 '12 at 14:22
  • @LindsayFisher, I've assumed that you have to deserialize more than one instrument tag, so I've edited my answer accordingly. Please test it again and tell me the result. – Felipe Feb 28 '12 at 15:10
  • Komyg, I tried the changed code, but I'm only get one instance of the instrument, not a collection. I'm expecting 13 instruments in the list. – Lindsay Fisher Feb 29 '12 at 07:03
  • Komyg, I tried this a well [XmlElement("instrument")] public Instrument instrument { get; set; } [XmlArray] [XmlArrayItem(ElementName = "instrument")] public List Instruments { get; set; } – Lindsay Fisher Feb 29 '12 at 07:05
  • Well, I've ran the code above with the XML example that you've posted and could deserialize more than one element without any problems. Could you edit your question and post an XML with more than one instrument element? Also, I've edited my answer with a sample code to test the XML deserialization. – Felipe Feb 29 '12 at 13:16
  • Komyg, thanks the code worked. I had unnecessary properties of my previous attempts which I now removed. – Lindsay Fisher Mar 01 '12 at 10:58
  • Can you help me this problem which was an oversight. I receive another file from the same vendor but the layout is different. I can deserialize the file I'm not picking up the field and it's values. – Lindsay Fisher Mar 01 '12 at 11:03
  • @LindsayFisher I (and everyone else at Stack Overflow) can help you, however the issue that you described on this question was solved, so I would recommed that you open another question for your other issue. – Felipe Mar 01 '12 at 13:48
3

Try this code.

var xml = System.IO.File.ReadAllText("test.xml");
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
    XmlSerializer serializer = new XmlSerializer(typeof(delayedquotes));
    delayedquotes data = (delayedquotes) serializer.Deserialize(stream);
}
animuson
  • 53,861
  • 28
  • 137
  • 147
Denis Gursky
  • 138
  • 5
  • Hello Marco, just got back into the office. thanks for the feedback will test the code. – Lindsay Fisher Feb 28 '12 at 13:42
  • Marco, tried your code and it worked, however, I tried the following, which I got from a tutorial. Can this be done? XmlSerializer serializer = new XmlSerializer(typeof(List)); List ExchangeRates = (List)serializer.Deserialize(stream); – Lindsay Fisher Feb 28 '12 at 14:06
0

I am not sure how you're deserializing the XML text into your object, but the following worked fine for me:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace XMLSerializationTest
{

    [Serializable, XmlRoot("delayedquotes"), XmlType("delayedquotes")]
    public class delayedquotes
    {
        [XmlElement("instrument")]
        public string instrument { get; set; }
        [XmlElement("title")]
        public string title { get; set; }
        [XmlElement("bid")]
        public double bid { get; set; }
        [XmlElement("spot")]
        public double spot { get; set; }
        [XmlElement("close")]
        public double close { get; set; }
        [XmlElement("b_time")]
        public DateTime b_time { get; set; }
        [XmlElement("o_time")]
        public DateTime o_time { get; set; }
        [XmlElement("time")]
        public DateTime time { get; set; }
        [XmlElement("hi")]
        public string hi { get; set; }
        [XmlElement("lo")]
        public string lo { get; set; }
        [XmlElement("offer")]
        public double offer { get; set; }
        [XmlElement("trade")]
        public double trade { get; set; }

        public delayedquotes()
        {

        }
    }

    static class Program
    {
        static void Main(string[] args)
        {
            string source = @"<delayedquotes id=""TestData""> 
  <headings> 
    <title/> 
    <bid>bid</bid> 
    <offer>offer</offer> 
    <trade>trade</trade> 
    <close>close</close> 
    <b_time>b_time</b_time> 
    <o_time>o_time</o_time> 
    <time>time</time> 
    <hi.lo>hi.lo</hi.lo> 
    <perc>perc</perc> 
    <spot>spot</spot> 
  </headings> 
  <instrument id=""Test1""> 
    <title id=""Test1"">Test1</title> 
    <bid>0</bid> 
    <offer>0</offer> 
    <trade>0</trade> 
    <close>0</close> 
    <b_time>11:59:00</b_time> 
    <o_time>11:59:00</o_time> 
    <time>11:59:00</time> 
    <perc>0%</perc> 
    <spot>0</spot> 
  </instrument> 
</delayedquotes> 
";
            var buff = Encoding.ASCII.GetBytes(source);
            var ms = new MemoryStream(buff);
            var xs = new XmlSerializer(typeof(delayedquotes));
            var o = (delayedquotes)xs.Deserialize(ms);

            Console.WriteLine("Title = {0}", o.instrument);
        }
    }
}
Rich Turner
  • 10,800
  • 1
  • 51
  • 68
  • Hello Richard, just got back into the office. thanks for the feedback will test the code. – Lindsay Fisher Feb 28 '12 at 13:42
  • Richard, I think I've the problem but I don't know how to solve it. I used the following code which I got from a tutorial. The xml file has multiple records. Can this be done? XmlSerializer serializer = new XmlSerializer(typeof(List)); List ExchangeRates = (List)serializer.Deserialize(stream); – Lindsay Fisher Feb 28 '12 at 14:13