type or namespace name 'Serializable' does not exist in the namespace 'System' (are you missing an assembly reference?
Solution 1
There is no System.Serializable
namespace; the SerializableAttribute
class is in the System
namespace. You should just write using System;
.
I think you're confusing C# using
statements with Java import
statement. In Java, you import specific classes (or all classes from a package with *
). In C#, the using
statement imports all types in the specified namespace.
Currently, specifying a class name after using
is an error, but in C# 6 it will be possible, and will import all static members from the class.
Other issue: you wrote Datamember
instead of DataMember
; C# is case sensitive. And anyway, DataMember
has no effect on a class that doesn't have the DataContract
attribute.
Also make sure you have referenced the System.Runtime.Serialization
assembly.
Solution 2
I got the same problem and by adding this resolved the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Comments
-
Sss almost 2 years
I am VS 2012 silverlight-5 beginner. I tried to serialize and de-serialize from a xml file. I have following error while doing this :
The type or namespace name 'Serializable' does not exist in the namespace 'System' (are you missing an assembly reference?) The type or namespace name 'SerializableAttribute' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'DatamemberAttribute' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'Datamember' could not be found (are you missing a using directive or an assembly reference?)
My code for serialization is :
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; namespace Model.XML { public static class Serialization<T> where T : class { public static T DeserializeFromXmlFile(string fileName) { if (!File.Exists(fileName)) { return null; } DataContractSerializer deserializer = new DataContractSerializer(typeof(T)); using (Stream stream = File.OpenRead(fileName)) { return (T)deserializer.ReadObject(stream); } } } }
But i am interested in having each class seperately (not all the 3 classes (parameter,component and attribute) obtained in one class).
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.Xml.Serialization.XmlTypeAttribute; using System.Serializable; //It gives red line under Serializable namespace Model.XML { [Serializable] //It gives red line public class attribute { [Datamember] //It gives red line public string name { get; set; } [Datamember] //It gives red line public string label { get; set; } [Datamember] //It gives red line public string unit { get; set; } [Datamember] //It gives red line public component thisComponent { get; set; } } }
i tried to reference
using System.Serializable;
but it is not available in the dll list i have in my VS2012. What is the cause of problem and how to get a rid of it ? Thanks in advance.EDIT AFTER Comments:
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; 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() { attribute Obj1 = Serialization<attribute>.DeserializeFromXmlFile("C:\\Users\\SHEK\\Desktop\\VannakNew\\DEV_CENTER\\Model\\XML\\XmlParameter.xml"); // Obj1.type = "shekhar"; } } }
And serialization.cs is :
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; namespace Model.XML { public static class Serialization<T> where T : class { public static T DeserializeFromXmlFile(string fileName) { if (!File.Exists(fileName)) { MessageBox.Show("File Path is wrong"); return null; } DataContractSerializer deserializer = new DataContractSerializer(typeof(T)); using (Stream stream = File.OpenRead(fileName)) { return (T)deserializer.ReadObject(stream); } } } }
Xml file is XmlParameter.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>
**It is working without error but i wonder why it always null object(on debugging) even the path of XmlParameter.xml file is correct. Is it due to using "[DataContract]" instead of [Serialized] ? because it si silverlight application. Actually i have todisplay the elements inside the nodes like,i have to display
"XAUUSD"
in myattribute.cs
class which present in node<ccypair>XAUUSD</ccypair>
** -
Thomas Levesque about 10 years@user234839, remove the
using System.Serializable;
statement -
Sss about 10 yearsI dont it but still those eror persists
-
Thomas Levesque about 10 years@user234839, which platform are you targeting? Is it a Windows Store or Windows Phone app?
-
Sss about 10 yearsHav you understood the question ? I have an xml file and i create seralizer using c# (i have written the code for that) but i want to seperate each class(instead all the 3 classes in one class) and "attribute" class is the first class from which i started and it gives those errors.
-
Sss about 10 yearsDatamEMBER ISSUE IS SOLVED nOW JUST SERALIZABLE IS left.
-
Thomas Levesque about 10 years@user234839,
SerializableAttribute
is not available in Silverlight. Just useDataContract
instead -
Sss about 10 yearsIt solved the error currently but the problem is when i try to do Serialization<YourCustomObject>.DeserializeFromXmlFile(MyFileNameOrPath); in "attribute" class. Then will it work (in fact this line gives error)? what should be the alternative. And Serializable is not supported in silverlight. What should i use now ?
-
Sss about 10 yearsI write this line : Serialization<YourCustomObject>.DeserializeFromXmlFile("XmlParameter.xml"); And the errors are as below
-
Sss about 10 yearsFirst :Error 1 Invalid token '(' in class, struct, or interface member declaration C:\Users\SHEK\Desktop\VannakNew\DEV_CENTER\Model\XML\attribute.cs Second : Error 2 The type or namespace name 'YourCustomObject' could not be found (are you missing a using directive or an assembly reference?) C:\Users\SHEK\Desktop\VannakNew\DEV_CENTER\Model\XML\attribute.cs Third :Error 3 'Model.XML.Serialization<T>.DeserializeFromXmlFile(string)' is a 'method' but is used like a 'type' C:\Users\SHEK\Desktop\VannakNew\DEV_CENTER\Model\XML\attribute.cs
-
Thomas Levesque about 10 years@user234839, where do you write this code? Please edit your question to show the new code for the attribute class. What what is YourCustomObject? Does it even exist? The compiler seems to think it doesn't...
-
Sss about 10 yearsThanks for trying to help me.I have just edited the question. I solved these errors (It was because i was not writting that inside static void main() function). But the problem is it always returns null object (Is there any problem remated to xml file path ? Because it always popups the MessageBox which i have in Serialization.cs class)
-
Sss about 10 yearsYou can have a look i have included xml file and other 2 files.
-
Thomas Levesque about 10 years@user234839, well, the only possible cause is that the file you specified doesn't exist... But anyway, if the
attribute
element is not the root of the XML, trying to deserialize it to classattribute
won't work... you must deserialize toparameter
instead -
Sss about 10 yearsBut the file exist there. Even all these classes files and xml files are in same folder
-
Thomas Levesque about 10 years@user234839, well, I don't think there's a bug in File.Exists... it would have been found a loooong time ago. So, the bug is somewhere else. Are you really 100% sure the path is correct?
-
Sss about 10 yearsThis path i have got fromhe properites click of xml file which i copied and pasted: C:\\Users\\SHEK\\Desktop\\VannakNew\\DEV_CENTER\\Model\\XML\\XmlParameter.xml"