在xml的反序列化中,在Visual Studio中发生xaml解析异常以创建对象

时间:2014-04-30 12:52:16

标签: c# xml silverlight deserialization xml-deserialization

我正在处理 c# using silverlight-5 in VS2012 并尝试派生。

我的代码如下:

Filename is `attribute.cs`
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Runtime.Serialization.Json;
    using System.Runtime;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
    using System.IO;
    using System.Collections.Generic;
    using System.Diagnostics;



    namespace Model.XML
    {
        [DataContract]
        public class attribute
        {
            [DataMember]
            public string type { get; set; }

            [DataMember]
            public string displayed { get; set; }

            [DataMember]
            public string add_remove { get; set; }

            [DataMember]
            public string ccypair { get; set; }

            [DataMember]
            public List<int> item { get; set; }

            public static void Main()
            {
               // System.IO.StreamReader myFileStream = new System.IO.StreamReader("C:\\Users\\SHEK\\Desktop\\VannakNew\\DEV_CENTER\\Model\\XML\\XmlParameter.xml");
                FileStream myFileStream = new FileStream(@"\XmlParameter.xml", FileMode.Open); //On debugging after this line the code breaks and generate exception
                attribute mainobj = null;
                XmlSerializer ser = new XmlSerializer(typeof(attribute));
                mainobj = ser.Deserialize(myFileStream) as attribute;
                Debug.WriteLine(mainobj.type);  
            }        
        }
    }

enter image description here 在行FileStream myFileStream = new FileStream(@"C:\Users\SHEK\Desktop\VannakNew\DEV_CENTER\DEV_CENTER\Parameters.xaml", FileMode.Open);之后,它切换到MainPage.Xaml.cs,并显示此异常。

为什么会这样做? 我要做的是: 我有一个xml文件,在xml文件中有一个名为attricute.cs的类,我必须显示它的节点元素。 xml文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<parameter>
  <name>bands_amounts</name>
  <label>Bands Amounts</label>
  <unit></unit>
  <component>
    <type>List</type>
    <attributes>
      <type>Integer</type>
      <displayed>4</displayed>
      <add_remove>yes</add_remove>
      <item>1 000 000</item>
      <item>5 000 000</item>
      <item>10 000 000</item>
      <item>20 000 000</item>
    </attributes>
    <attributes>
      <ccypair>XAUUSD</ccypair>
      <item>100</item>
      <item>500</item>
      <item>1000</item>
    </attributes>
  </component >
</parameter>

评论后编辑:我在阅读评论后将我的代码更改为此,但问题是在FileStream myFileStream = new FileStream(@"\XmlParameter.xml", FileMode.Open);之后它甚至不再执行任何行,它会再次直接显示该异常。

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;



namespace Model.XML
{
   [XmlRoot("parameter")]

    public class attribute
    {
        [DataMember]
        public string type { get; set; }

        [DataMember]
        public string displayed { get; set; }

        [DataMember]
        public string add_remove { get; set; }

        [DataMember]
        public string ccypair { get; set; }

        [DataMember]
        public List<int> item { get; set; }

        public static void Main()
        {
           // System.IO.StreamReader myFileStream = new System.IO.StreamReader("C:\\Users\\SHEK\\Desktop\\VannakNew\\DEV_CENTER\\Model\\XML\\XmlParameter.xml");
            FileStream myFileStream = new FileStream(@"\XmlParameter.xml", FileMode.Open);

            attribute mainobj = null;
            XmlSerializer ser = new XmlSerializer(typeof(parameter));
            mainobj = ser.Deserialize(myFileStream) as attribute;
            Debug.WriteLine(mainobj.type);  
        }        
    }
}

1 个答案:

答案 0 :(得分:1)

作为根类型传递给XmlSerializer的类型表示xml的根。您正在通过typeof(attribute)。这种类型与xml不匹配;你应该传递typeof(Parameter),即一个看起来像xml的类型。例如:

[XmlRoot("parameter")]
public class Parameter {
    [XmlElement("component")]
    public Component Component {get;set;}
}

(编辑,见评论)另一个文件

public class Component {
    [XmlElement("attributes")]
    public List<Attribute> Attributes {get;set;}
}

使用:

XmlSerializer ser = new XmlSerializer(typeof(Parameter));
var mainobj = (Parameter)ser.Deserialize(myFileStream);