-1

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);  
        }        
    }
}
Sss
  • 1,519
  • 8
  • 37
  • 67
  • BTW; `XmlSerializer` does not use `DataMember` attributes. – Marc Gravell Apr 30 '14 at 12:58
  • [I give out -1s like hot cakes to people who insist on showing us pictures of exception helper dialogs. Learn how to use them correctly.](http://i.stack.imgur.com/JNABr.png) –  Apr 30 '14 at 13:00
  • I am using silverlight . It dont support "[Serialize]" – Sss Apr 30 '14 at 13:00
  • @user234839 that's fine, `XmlSerializer` doesn't use `[Serializable]` either; I didn't mention `[Serialize]` or `[Serializable]` – Marc Gravell Apr 30 '14 at 13:02

1 Answers1

1

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);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • But i want to have different files for each class like seperate class for component, seperate for attribute and parameter. All classes must not be in same file – Sss Apr 30 '14 at 12:59
  • @user234839 do you mean the C# files? then you create multiple files and you put the class or classes that you want in each file. That is purely organisational, and does not change the answer in the slightest. – Marc Gravell Apr 30 '14 at 13:02
  • yES ;;i MEAN attribute.cs, component.cs and parameter.cs all seperately. and i am using silverlight-5 for this. – Sss Apr 30 '14 at 13:03
  • @user234839 so... just do that? See the edit. The code doesn't need to be in one file - showing them next to each-other was purely for convenience (actually, I didn't even mention files) – Marc Gravell Apr 30 '14 at 13:04
  • you mean. your first code is in parameter.cs class and second code is in component.cs class. Could you please edit and mention the class name as well. Like there would be 3 classes here parameter, component and attribute. and how and where i will call deseralze function ? if i a=have to print "Integer" of "Integer" which is inside attribute. – Sss Apr 30 '14 at 13:08
  • @user234839 no, I'm sure you can simply *imagine* different classes going into different files; there is no need for me to add filenames, because **files do not matter** to the compiler. I have shown **how** to call the deserialize method; *where* you should call it is entirely up to your code, but I'm going to say "at the point where you want to deserialize". I cannot parse the last question about "a=have to print". – Marc Gravell Apr 30 '14 at 13:10
  • I have again edited the code and changed my code to follow you. But the problem is it even dont execute any line after "FileStream myFileStream = new FileStream(@"\XmlParameter.xml", FileMode.Open);" it directly again gives same exception. (it even dont access the second line which is "attribute mainobj = null;") – Sss Apr 30 '14 at 13:19
  • @user234839 if it doesn't get past the line that opens the file, then the problem is not deserialization. You will need to find out why the file isn't opening (does it exist? is it locked?). I cannot magically tell you. – Marc Gravell Apr 30 '14 at 13:29
  • i am not able to understand your code. Where are you writing these lines "XmlSerializer ser = new XmlSerializer(typeof(Parameter)); var mainobj = (Parameter)ser.Deserialize(myFileStream);" .IS IT INSIDE attribute.cs class file?c Is this Object "mainobj" belongs to the object of attribute class ? – Sss Apr 30 '14 at 13:31
  • 1
    @user234839 it **does not matter** where that code is; that goes where-ever you need to do the deserialization. I cannot tell you where you want to do deserialization: that is up to you. But specifically, it is "wherever it is in your example" - just with two lines changed. – Marc Gravell Apr 30 '14 at 13:32
  • I am sorry you even double confused me.This exception keeps on persisting. What i asked is very simple:(a) suppose i have any xml content stored in a string(lets say "xmlstring").(b) I have 3 different files parameter.cs, componenet.cs and attribute.cs which contain s their respective name class. I am working inside attrbute class which is in attribute.cs NOW QUESTION COMES TO MIND IS (1) When and where i have to put the data inside these square brackets [WhatShouldBeWrittenInsideThem] for each class. – Sss Apr 30 '14 at 15:41
  • My code inside the attribute class main function is: "public static void Main() { string xmlstring = @""; XmlSerializer serializer = new XmlSerializer(typeof(parameter)); XmlReader reader = XmlReader.Create(new StringReader(xmlstring)); attribute Object1 = (attribute)serializer.Deserialize(reader); Debug.WriteLine(Object1.type); } " – Sss Apr 30 '14 at 15:42
  • @user234839 the code I have posted in my answer, along with the class in the question (I edited `attribute` to `Attribute`, but that's all) works successfully to deserialize the xml shown in the question. – Marc Gravell Apr 30 '14 at 15:42
  • `attribute Object1 = (attribute)serializer.Deserialize(...)` - why? It is deserializing a `Parameter`, not an `attribute`? If that succeeds in deserializing, it will fail with a type cast exception. You will notice that I cast to `Parameter` in the code shown in the answer. – Marc Gravell Apr 30 '14 at 15:44
  • I have doubt in your explanation. If you see my xml code there is a list of which i handle in c# code liek this public List attribut { get; set; } but when i do "Debug.WriteLine(attrib.item);" where it is inside "var Object1 = (parameter)deserializer.Deserialize(reader); foreach (var attrib in Object1.component.attribut)" THEN ON DEBUGGING IT DONT SHOW "1 000 000" and "5 000 000" etc. How to see thse values because i have to use them in my code. – Sss May 02 '14 at 13:03
  • i still have problem with it. could you please see my this question ? http://stackoverflow.com/questions/23429534/how-to-deserialize-the-xmlelement-containing-list-in-xml Thanks a lot. – Sss May 02 '14 at 15:09