Question

I am working on c# using silverlight-5 in VS2012 and trying to derialize.

My code to do this is as follows :

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 Just after the line FileStream myFileStream = new FileStream(@"C:\Users\SHEK\Desktop\VannakNew\DEV_CENTER\DEV_CENTER\Parameters.xaml", FileMode.Open); it switches to the MainPage.Xaml.cs and there it shows this exception .

Why it do so ? What i hav to do is : I have an xml file and there is a class called attricute.cs in xml file i have to display it's node elements. The xml file is as follows:

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

EDIT AFTER COMMENTS: I changed my code to this after reading the comments but the problem is it even dont executes any line after FileStream myFileStream = new FileStream(@"\XmlParameter.xml", FileMode.Open); it directly show that exception again.

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);  
        }        
    }
}
Was it helpful?

Solution

The type that you pass to XmlSerializer as the root type represents the root of the xml. You are passing typeof(attribute). This type does not match the xml; you should be passing typeof(Parameter) i.e. a type that looks like the xml. For example:

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

(edit, see comments) another file

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

with:

XmlSerializer ser = new XmlSerializer(typeof(Parameter));
var mainobj = (Parameter)ser.Deserialize(myFileStream);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top