casting a string from xml to class object - c#

I want to cast a string from xml file to be casted to ContactPersonType
See for loop for where string needs to be casted
public class ContactPersonType
{
private String _id;
public String ID
{
get { return _id; }
set { _id = value; }
}
private String _name;
public String Name
{
get { return _name; }
set { _name = value; }
}
}
//ContactPerson class
private ContactPersonType _jobRole;
public ContactPersonType JobRole
{
get { return _jobRole; }
set { _jobRole = value; }
}
public static ObservableCollection<ContactPerson> getContactPerson()
{
ObservableCollection<ContactPerson> ContactPersons = new ObservableCollection<ContactPerson>();
XmlDocument doc = new XmlDocument();
doc.Load("contactpersoon.xml");
XmlNodeList elemList = doc.GetElementsByTagName("contact");
for (int i = 0; i < elemList.Count; i++)
{
//this needs to be casted to ContactPersonType
contactPerson.JobRole = elemList[i]["jobrole"].InnerText;
}
return ContactPersons;
}

I'm not really familliar with the way you read the XML elements so this code might need some tweaking but this should be along the lines of what you're looking for (also took the liberty of making your properties autoimplemented for higher code readability)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace Program
{
public class ContactPersonType
{
public string ID { get; set; }
public string Name { get; set; }
}
public class ContactPerson
{
public ContactPersonType JobRole { get; set; }
public static ObservableCollection<ContactPerson> GetContactPerson()
{
var contactPersons = new ObservableCollection<ContactPerson>();
XElement doc = XElement.Load("contactpersoon.xml");
var contacts = doc.Elements("contact");
for (int i = 0; i < contacts.Count(); i++)
{
contactPersons.Add(new ContactPerson
{
JobRole = new ContactPersonType
{
ID = i.ToString(),
Name = contacts.ElementAt(i).Element("jobrole").Value
}
});
}
return contactPersons;
}
}
}
Depending on how you set the ID property you could possibly rewrite the loop as a foreach. For more info on XElement and it's members, check out http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx and http://msdn.microsoft.com/en-us/vstudio/bb688087.aspx

Can you not deserialize the XML to the .NET class with a helper such as the one shown below?
public T Deserialize<T>(string fileName)
{
try
{
XmlSerializer sx = new XmlSerializer(typeof(T)); //or T.GetType?
StreamReader sr = new StreamReader(fileName);
var data = sx.Deserialize(sr);
sr.Close();
return (T)data;
}
catch (Exception ex)
{
throw;
}
}
I hate XmlDocument, so always deserialize where possible (though a potential alternative to i do like is XDocument).
EDIT - the usage would be something like:
var item = Deserialize<ContactPersonType>(fileName);

Related

Ho do I ordering XMLArray Items of two different object list in alternate order?

I have class Type which having Two list of different object types. When I am trying to serialize the Class Type I am getting list in sequence but I want to get the list of items in alternate order.
public class Type
{
private List<Slow> slowField;
private List<Fast> FastField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("SLOW", Order=1)]
public List<Slow> SLOW
{
get
{
return this.slowField;
}
set
{
this.slowField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FAST", Order=2)]
public List<Fast> FAST
{
get
{
return this.FastField;
}
set
{
this.FastField = value;
}
}
}
Current Output :
<Type>
<SLOW>S1</SLOW>
<SLOW>S2</SLOW>
<SLOW>S3</SLOW>
<FAST>F1</FAST>
<FAST>F2</FAST>
<FAST>F3</FAST>
</Type>
I need this Output:
<Type>
<SLOW>S1</SLOW>
<FAST>F1</FAST>
<SLOW>S2</SLOW>
<FAST>F2</FAST>
<SLOW>S3</SLOW>
<FAST>F3</FAST>
</Type>
One approach would be to introduce a third Collection which would combine the two existing collections in desired order. For example,
Assuming the structure of Fast and Slow are as follows
public class Fast
{
public int Value{get;set;}
}
public class Slow
{
public int Value{get;set;}
}
You could now introduce a Base Class (or use System.Object as common base) for both types. For example,
public class Base{}
public class Fast:Base
{
public int Value{get;set;}
}
public class Slow:Base
{
public int Value{get;set;}
}
Now you could add a third collection in your Type Class as following
[XmlElement("SLOW", Type = typeof(Slow))]
[XmlElement("FAST", Type = typeof(Fast))]
public List<Base> Complete => GetMergedList();
private List<Base> GetMergedList()
{
int minLen = Math.Min(SLOW.Count, FAST.Count);
var list = SLOW.Take(minLen)
.Zip(FAST.Take(minLen), (a, b) => new Base[] { a, b })
.SelectMany(array => array)
.Concat(SLOW.Skip(minLen))
.Concat(FAST.Skip(minLen));
return list.ToList();
}
Note that the code assumes Fast and Slow collections are already sorted individually, but if not, need to add logic sorting them.
You would also need to mark the existing properties with XmlIgnoreAttribute to skip them during serialization.
[XmlIgnore]
public List<Slow> SLOW
{
....
}
[XmlIgnore]
public List<Fast> SLOW
{
....
}
Now you could serialize the Type instance to get the desired result.
Demo Code
Just alternate the output, if the iteration is odd print the first list if even print the second one. Do someting like this :
for( int i = 1; i < ListLength; i++)
{
if( i%2 == 0)
{
// put here your second list dont forget to put i-1 since we started with 1 in for
print(" The value is even");
}
else
{
//put here your first list
print("the value is odd");
}
}
Here is a complex solution using IXmlSerialize. I don't know the details of the classes Fast and Slow. So I'm just sorting the innertext of the xml element
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
Type test = new Type()
{
FAST = new List<Fast>() { new Fast() { value = "F1" }, new Fast() { value = "F2" }, new Fast() { value = "F3" } },
SLOW = new List<Slow>() { new Slow() { value = "S1" }, new Slow() { value = "S2" }, new Slow() { value = "S3" } }
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Type));
serializer.Serialize(writer, test);
}
}
public class Fast
{
public string value { get; set; }
}
public class Slow
{
public string value { get; set; }
}
public class Type : IXmlSerializable
{
private List<Slow> slowField;
private List<Fast> FastField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("SLOW", Order = 1)]
public List<Slow> SLOW
{
get
{
return this.slowField;
}
set
{
this.slowField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FAST", Order = 2)]
public List<Fast> FAST
{
get
{
return this.FastField;
}
set
{
this.FastField = value;
}
}
public void WriteXml(XmlWriter writer)
{
string pattern = #"(?'prefix'.*)(?'suffix'\d+)";
XmlSerializer fastSerializer = new XmlSerializer(typeof(Fast));
XmlSerializer slowSerializer = new XmlSerializer(typeof(Slow));
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.Indent = true;
MemoryStream ms = new MemoryStream();
XmlWriter childWriter = XmlWriter.Create(ms,settings);
childWriter.WriteRaw("<Root>");
foreach(Slow slow in slowField)
{
slowSerializer.Serialize(childWriter,slow);
//childWriter.WriteRaw("\n");
}
foreach (Fast fast in FastField)
{
fastSerializer.Serialize(childWriter, fast);
//childWriter.WriteRaw("\n");
}
childWriter.WriteRaw("</Root>");
childWriter.Flush();
ms.Position = 0; ;
string text = Encoding.UTF8.GetString(ms.GetBuffer());
XElement rootElement = XElement.Load(ms);
var orderChildren = rootElement.Elements()
.Select(x => new { match = Regex.Match((string)x, pattern), element = x })
.Select(x => new { prefix = x.match.Groups["prefix"].Value, suffix = x.match.Groups["suffix"].Value, element = x.element })
.OrderBy(x => x.suffix)
.ThenBy(x => x.prefix)
.ToList();
foreach(var orderChild in orderChildren)
{
orderChild.element.WriteTo(writer);
}
}
public void ReadXml(XmlReader reader)
{
XmlSerializer fastSerializer = new XmlSerializer(typeof(Fast));
XmlSerializer slowSerializer = new XmlSerializer(typeof(Slow));
while (!reader.EOF)
{
if (reader.Name == "SLOW")
{
if (slowField == null) slowField = new List<Slow>();
slowField.Add((Slow)slowSerializer.Deserialize(reader));
}
if (reader.Name == "FAST")
{
if (FastField == null) FastField = new List<Fast>();
FastField.Add((Fast)slowSerializer.Deserialize(reader));
}
}
}
public XmlSchema GetSchema()
{
return (null);
}
}
}

XML deserialization with array inside a class

I'm trying to deserialize a string of XML data. But I got back no data in the object returned by the deserialization function.
Here are the class definitions:
[XmlType("STATE-COUNTY-RECORDS")]
public class StateCountyRecords
{
[XmlElement("TOTAL")]
public int Total { get; set; }
[XmlElement("LIMIT")]
public int Limit { get; set; }
[XmlElement("OFFSET")]
public int Offset { get; set; }
[XmlArray("STATE-COUNTY-ARRAY"), XmlArrayItem("STATE-COUNTY")]
public StateCounty[] StateCountyArray { get; set; }
public StateCountyRecords() {}
public StateCountyRecords(int total, int limit, int offset, int[] id, string[] fips_code, string[] state, string[] name)
{
Total = total;
Limit = limit;
Offset = offset;
var count = id.Length;
StateCountyArray = new StateCounty[count];
for (int i = 0; i < count; i++)
{
StateCountyArray[i] = new StateCounty(id[i], fips_code[i], state[i], name[i]);
}
}
}
public class StateCounty
{
[XmlElement("ID")]
public int Id { get; set; }
[XmlElement("FIPS-CODE")]
public string Fips_Code { get; set; }
[XmlElement("STATE")]
public string State { get; set; }
[XmlElement("NAME")]
public string Name { get; set; }
public StateCounty(int id, string fips_code, string state, string name)
{
Id = id;
Fips_Code = fips_code;
State = state;
Name = name;
}
public StateCounty() { }
}
Here is the xml string data:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<STATE-COUNTY-FILE xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<STATE-COUNTY-RECORDS>
<TOTAL>3314</TOTAL>
<LIMIT>10</LIMIT>
<OFFSET>0</OFFSET>
<STATE-COUNTY-ARRAY>
<STATE-COUNTY>
<ID>1</ID>
<FIPS-CODE>01000</FIPS-CODE>
<STATE>AL</STATE>
<NAME>Alabama</NAME>
</STATE-COUNTY>
<STATE-COUNTY>
<ID>2</ID>
<FIPS-CODE>01001</FIPS-CODE>
<STATE>AL</STATE>
<NAME>Autauga</NAME>
</STATE-COUNTY>
<STATE-COUNTY>
<ID>3</ID>
<FIPS-CODE>01003</FIPS-CODE>
<STATE>AL</STATE>
<NAME>Baldwin</NAME>
</STATE-COUNTY>
</STATE-COUNTY-ARRAY>
</STATE-COUNTY-RECORDS>
</STATE-COUNTY-FILE>
Here is the function to deserialize the xml string:
public static StateCountyRecords DeserializeStateCountyData(string xmlString)
{
XmlSerializer mySerializer = new XmlSerializer(typeof(StateCountyRecords), new XmlRootAttribute("STATE-COUNTY-FILE"));
using (XmlReader myXmlReader = XmlReader.Create(new StringReader(xmlString)))
{
StateCountyRecords rslt;
rslt = (StateCountyRecords)mySerializer.Deserialize(myXmlReader);
}
}
}
The rslt I got back contains no data with Total and Limit, and Offset all being 0 and StateCountyArray being null. I didn't get any error. I made sure that the xml is valid by:
if (myXmlReader.CanResolveEntity && myXmlReader.CanReadValueChunk && myXmlReader.CanReadBinaryContent)
rslt = (StateCountyRecords)mySerializer.Deserialize(myXmlReader);
I wonder if there is a mismatch between the XmlElement of my class definition and the XML file.
Your problem is that your XML has an outer element that is not accounted for in your data model, namely the <STATE-COUNTY-FILE> element:
<STATE-COUNTY-FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<STATE-COUNTY-RECORDS>
<!-- Various elements under StateCountyRecords -->
</STATE-COUNTY-RECORDS>
</STATE-COUNTY-FILE>
Since the root of your data model is StateCountyRecords there is nothing that corresponds to this outermost element. You try to solve this by using new XmlRootAttribute("STATE-COUNTY-FILE"), however this will only rename the outer element, not add the extra level of nesting.
Instead, you need to introduce an outer container POCO for this purpose:
[XmlType("STATE-COUNTY-FILE")]
public class StateCountyFile
{
[XmlElement("STATE-COUNTY-RECORDS")]
public StateCountyRecords StateCountyRecords { get; set; }
}
And deserialize as follows:
public static StateCountyRecords DeserializeStateCountyData(string xmlString)
{
var mySerializer = new XmlSerializer(typeof(StateCountyFile));
using (var myXmlReader = XmlReader.Create(new StringReader(xmlString)))
{
var rslt = (StateCountyFile)mySerializer.Deserialize(myXmlReader);
return rslt.StateCountyRecords;
}
}
Alternatively, you could manually advance the XmlReader until encountering an element with the name <STATE-COUNTY-RECORDS>, and deserialize that:
public static StateCountyRecords DeserializeStateCountyData(string xmlString)
{
return XmlTypeExtensions.DeserializeNestedElement<StateCountyRecords>(xmlString);
}
Using the extension methods:
public static class XmlTypeExtensions
{
public static T DeserializeNestedElement<T>(string xmlString)
{
var mySerializer = new XmlSerializer(typeof(T));
var typeName = typeof(T).XmlTypeName();
var typeNamespace = typeof(T).XmlTypeNamespace();
using (var myXmlReader = XmlReader.Create(new StringReader(xmlString)))
{
while (myXmlReader.Read())
if (myXmlReader.NodeType == XmlNodeType.Element && myXmlReader.Name == typeName && myXmlReader.NamespaceURI == typeNamespace)
{
return (T)mySerializer.Deserialize(myXmlReader);
}
return default(T);
}
}
public static string XmlTypeName(this Type type)
{
var xmlType = type.GetCustomAttribute<XmlTypeAttribute>();
if (xmlType != null && !string.IsNullOrEmpty(xmlType.TypeName))
return xmlType.TypeName;
return type.Name;
}
public static string XmlTypeNamespace(this Type type)
{
var xmlType = type.GetCustomAttribute<XmlTypeAttribute>();
if (xmlType != null && !string.IsNullOrEmpty(xmlType.Namespace))
return xmlType.Namespace;
return string.Empty;
}
}
Incidentally, deserialization problems such as this are easily diagnosed by creating an instance of your class in memory, serializing it, then comparing the resulting XML with the input XML. Usually the problem is apparent.

How to get value inside XML tag?

I have the follow XML structure:
<Document>
<Sectors>
<Sector>
SectorName1
<Subsectors>
<Subsector>Subsector1</Subsector>
<Subsector>Subsector2</Subsector>
</Subsectors>
</Sector>
<Sector>
SectorName2
<Subsectors>
<Subsector>Subsector1</Subsector>
<Subsector>Subsector2</Subsector>
</Subsectors>
</Sector>
</Sectors>
</Document>
Also I have classes for deserialize:
public class MetaDataXML
{
public class SectorXML
{
[XmlArrayItem(ElementName = "Sector")]
string SectorName { get; set; }
[XmlArray]
[XmlArrayItem(ElementName = "Subsector")]
public List<string> Subsectors { get; set; }
}
public List<SectorXML> Sectors { get; set; }
}
And part of code which do deserialize:
var xRoot = new XmlRootAttribute { ElementName = "Document", IsNullable = true };
var reader = new XmlSerializer(typeof(MetaDataXML), xRoot);
var data = (MetaDataXML)reader.Deserialize(streamXML);
After deserialization I successfully get subsectors velues, but I didn't get values for SectorName. How I need to organize my structure of class that I'll get values "SectorName1" and "SectorName2" for my string SectorName property?
I found that that this case it's a "Mixed Content". How we can parse this text values?
Whilst I am not entirely sure what it is you're trying to achieve here, I've made a few modifications to your XML class and provided some sample code below that is able to retrieve all of the information about a sector, including its name and the name of all the subsectors inside it.
XML Class:
namespace DocumentXml
{
[XmlRoot("Document")]
public class Document
{
[XmlArray("Sectors")]
[XmlArrayItem("Sector")]
public Sector[] Sectors { get; set; }
}
[XmlRoot("Sector")]
public class Sector
{
[XmlAttribute("SectorName")]
public string SectorName { get; set; }
[XmlArray("Subsectors")]
[XmlArrayItem("Subsector")]
public string[] Subsectors { get; set; }
}
}
Main Program Class:
namespace DocumentXml
{
class Program
{
static void Main(string[] args)
{
var path = #"D:\sandbox\DocumentXml\DocumentXml\Sample.xml";
var serializer = new XmlSerializer(typeof(Document));
var document = serializer.Deserialize(File.OpenRead(path)) as Document;
var sectors = document.Sectors;
foreach (var s in sectors)
{
Console.WriteLine($"Sector Name: {s.SectorName}");
foreach (var ss in s.Subsectors)
{
Console.WriteLine($"Subsector Name: {ss}");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Sample XML:
<Document>
<Sectors>
<Sector SectorName="SectorName1">
<Subsectors>
<Subsector>Subsector1</Subsector>
<Subsector>Subsector2</Subsector>
</Subsectors>
</Sector>
<Sector SectorName="SectorName2">
<Subsectors>
<Subsector>Subsector1</Subsector>
<Subsector>Subsector2</Subsector>
</Subsectors>
</Sector>
</Sectors>
</Document>
Output:
EDIT
Since the XML structure cannot be changed, this new class will preserve the structure and also allow you to get the value in question. XmlText returns everything inside the value so a custom set had to be used to ensure that the whitespace was correctly trimmed from it.
[XmlRoot("Document")]
public class MetaDataXml
{
[XmlArray("Sectors")]
[XmlArrayItem("Sector")]
public Sector[] Sectors { get; set; }
}
[XmlRoot("Sector")]
public class Sector
{
[XmlIgnore]
private string _sectorName;
[XmlText]
public string SectorName
{
get
{
return _sectorName;
}
set
{
_sectorName = value.Trim();
}
}
[XmlArray]
[XmlArrayItem(ElementName = "Subsector")]
public List<string> Subsectors { get; set; }
}
Sample Program:
class Program
{
static void Main(string[] args)
{
var path = #"D:\sandbox\DocumentXml\DocumentXml\Sample.xml";
using (var stream = File.OpenRead(path))
{
var deserializer = new XmlSerializer(typeof(MetaDataXml));
var data = (MetaDataXml)deserializer.Deserialize(stream);
foreach (var s in data.Sectors)
{
Console.WriteLine($"Sector Name: {s.SectorName}");
foreach (var ss in s.Subsectors)
{
Console.WriteLine($"Subsector Name: {ss}");
}
Console.WriteLine();
}
}
Console.ReadKey();
}
}

using generics with xml deserialisation

I want to pass an object into the resulting type of an xml deserialisation and maintain strong typing.
So the deserialisation class can take any type that implements the IResult interface which in this case is Result and Result2.
I have got this working by making the getObject method return dynamic but i would much rather keep the compile time checking and i think it should be possible.
I have tried using generics, as in the example below, but the deser.getObject(doc()); line gives me a "cannot be inferred from usage" compile error.
Thanks for all help.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace SOQuestion
{
class Program
{
static void Main(string[] args)
{
var deser = new Deserialised(new Result());
var result = deser.getObject(doc());
var deser2 = new Deserialised(new Result2());
var result2 = deser.getObject(doc());
Console.Writeline(result.status);
Console.Writeline(result2.status);
}
public XmlDocument doc()
{
var doc = new XmlDocument();
var el = (XmlElement)doc.AppendChild(doc.CreateElement("Result"));
el.SetAttribute("status", "ok");
el.SetAttribute("status2", "not ok");
return doc;
}
}
class Deserialised
{
private IResult result;
private Type resultType;
public Deserialised(IResult _result)
{
result = _result;
resultType = Type.GetType(result.GetType().AssemblyQualifiedName);
}
public T getObject<T>(XmlDocument xml)
{
var mySerializer = new XmlSerializer(resultType);
var myStream = new MemoryStream();
xml.Save(myStream);
myStream.Position = 0;
var r = mySerializer.Deserialize(myStream);
return (T)r;
}
}
interface IResult
{
public string status {get;set;}
}
[Serializable]
public class Result :IResult
{
[XmlAttribute]
public string status { get; set; }
}
[Serializable]
public class Result2 : IResult
{
[XmlAttribute]
public string status2 { get; set; }
}
}
Indeed, that isn't going to work - the compiler has no way of knowing the T from that. Remember that the T comes from the caller at compile-time, not from the result of the method at runtime. There are ways to switch between reflection/generics, but it is ugly and isn't going to help much here. I would just return object instead:
public object GetObject(XmlDocument xml) {
var mySerializer = new XmlSerializer(resultType);
using(var myStream = new MemoryStream()) {
xml.Save(myStream);
myStream.Position = 0;
return mySerializer.Deserialize(myStream);
}
}
and then let the caller handle the dynamic etc:
var deser = new Deserialised(new Result());
dynamic result = deser.GetObject(doc());
var deser2 = new Deserialised(new Result2());
dynamic result2 = deser.GetObject(doc());
Console.Writeline(result.status);
Console.Writeline(result2.status);
Because of the dynamic above, the .status in the two Console.WriteLine will still work.
In my opinion the answer you provided yourself overcomplicates things. (In addition to doing some other weird stuff, ... see my comment.)
There is nothing gained in using generics here. You can just as well use the following approach, and both of your requirements will still hold.
public IResult GetObject(XmlDocument xml)
{
var mySerializer = new XmlSerializer(resultType);
using (var myStream = new MemoryStream())
{
xml.Save(myStream);
myStream.Position = 0;
return (IResult)mySerializer.Deserialize(myStream);
}
}
... simply call it as follows:
var deser = new Deserialised(new Result());
var result = (Result)deser.getObject(doc());
var deser2 = new Deserialised(new Result2());
var result2 = (Result2)deser.getObject(doc());
Casting to anything which doesn't implement IResult will trigger a compiler error.
It is not really clear what you are trying to do here, but suppose you made Deserialised generic.
class Deserialised<T>
where T : IResult
{
private T result;
private Type resultType;
public Deserialised(T _result)
{
result = _result;
}
public T getObject(XmlDocument xml)
{
var mySerializer = new XmlSerializer(typeof(T));
var myStream = new MemoryStream();
xml.Save(myStream);
myStream.Position = 0;
var r = (T)mySerializer.Deserialize(myStream);
return r;
}
}
Why are you even passing _result as a parameter and storing it? My guess is you only needed it since you didn't know about typeof()? In that case simply drop it. After doing so again you just end up with a class which defines your generic parameter, with again the sole purpose to define the required cast.
So i have found a solution that my two requirements of 1. that passed in type implements IResult interface 2. that the returned object is statically typed.
A generic method with a constraint like below
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace SOQuestion
{
class Program
{
static void Main(string[] args)
{
var result = new Deserialised().getObject<Result>();
var result2 = new Deserialised().getObject<Result2>();
Console.WriteLine(result.status);
Console.WriteLine(result.errorMessage);
Console.ReadLine();
}
}
class Deserialised
{
public T getObject<T>() where T : IResult
{
try
{
var instance = Activator.CreateInstance<T>();
var mySerializer = new XmlSerializer(instance.GetType());
var myStream = new MemoryStream();
doc().Save(myStream);
myStream.Position = 0;
var r = mySerializer.Deserialize(myStream);
throw new DivideByZeroException();
return (T)r;
}
catch (Exception exp)
{
var instance = Activator.CreateInstance<T>();
instance.errorMessage = "something wrong here";
return instance;
}
;
}
public static XmlDocument doc()
{
var doc = new XmlDocument();
var el = (XmlElement)doc.AppendChild(doc.CreateElement("Result"));
el.SetAttribute("status", "ok");
el.SetAttribute("status2", "notok");
return doc;
}
}
interface IResult
{
string status { get; set; }
string errorMessage { get; set; }
}
[Serializable]
public class Result : IResult
{
[XmlAttribute]
public string status { get; set; }
[XmlAttribute]
public string errorMessage { get; set; }
[XmlAttribute]
public string message { get; set; }
}
[Serializable]
public class Result2 : IResult
{
[XmlAttribute]
public string status { get; set; }
[XmlAttribute]
public string message2 { get; set; }
[XmlAttribute]
public string errorMessage { get; set; }
}
[Serializable]
public class Result3
{
[XmlAttribute]
public string status { get; set; }
[XmlAttribute]
public string message2 { get; set; }
[XmlAttribute]
public string errorMessage { get; set; }
}
}

Object serializing/deserializing doesn't work

I have an extension method for System.Object to serialize and deserialize objects using Json.Net. this is my Extension methods:
public static void SaveToFile(this object data, string FileName)
{
using (StreamWriter writer = new StreamWriter(FileName))
{
string encode = WpfApplication.Helper.Encrypt(JsonConvert.SerializeObject(data));
writer.Write(encode);
writer.Close();
}
}
public static void LoadFromFile<t>(this object data, string FileName)
{
using (StreamReader reader = new StreamReader(FileName))
{
data = JsonConvert.DeserializeObject<t>(WpfApplication.Helper.Decrypt(reader.ReadToEnd()));
reader.Close();
}
}
and It's the class that I want to deserialize:
public class CardPack
{
#region Properties
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private List<FlashCard> cards;
public List<FlashCard> Cards
{
get { return cards; }
set { cards = value; }
}
private bool registered;
public bool Registered
{
get { return registered; }
set { registered = value; }
}
private int currentCardIndex;
public int CurrentCardIndex
{
get { return currentCardIndex; }
set { currentCardIndex = value; }
}
public string RegisterKey { get; set; }
public string ViewName { get; set; }
public List<FlashCard> TodayCards { get; set; }
#endregion
~CardPack()
{
foreach (FlashCard card in cards)
{
card.Check();
}
currentCardIndex = 0;
TodayCards = null;
this.SaveToFile(string.Format(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(#"file:\\")[1] + #"\Packs\{0}.json", name));
}
but whenever I deserialize the class cards is empty and I don't know how to resolve the problem. Can anybody help me?
Update
I find the error when I had this code:
public CardPack(string Name)
{
this.name = Name;
this.LoadFromFile<CardPack>(string.Format(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(#"file:\\")[1] + #"\Packs\{0}.json", name));
foreach (var item in cards)
{
if (item.NextTime == null)
{
int a = 0;
}
}
TodayCards = cards.Where(c => c.NextTime.Date == DateTime.Today).ToList();
}
because the application closed when it tries to run foreach (var item in cards)!
I asked here and found out that cards is empty!
update2 I serialized the CardPack object with a little different structure. in previous structure Cards property was read-only.
I found that data = JsonConvert.DeserializeObject<t>(WpfApplication.Helper.Decrypt(reader.ReadToEnd())); in extension method doesn't change to 'data' class then Cards in CardPack is always null. I'll ask a question to find out why I cant set the class from it's extension method later.

Categories

Resources