反序列化XML文件 - xml文档中的错误(1,2)

时间:2012-02-28 10:33:12

标签: c# xml xml-deserialization

我正在尝试反序列化我从XmlSerializer供应商处收到的XML文件,但是我得到了这个例外:There is an error in XML document (1, 2).InnerException Message "<delayedquotes xmlns=''> was not expected.。我搜索了stackoverflow论坛,谷歌并实施了建议,但我仍然得到相同的错误。请查看随附的xml文件的一些内容:

<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>

和代码

[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()
    {

    }
}

3 个答案:

答案 0 :(得分:3)

试试这段代码。

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);
}

答案 1 :(得分:2)

也许你可以试试这段代码:

[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; }
}

此外,这是一个测试上述类的示例代码:

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!");
}

答案 2 :(得分:0)

我不确定你是如何将XML文本反序列化到你的对象中的,但以下对我来说很好:

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);
        }
    }
}