First serialization success, second fails? Why? - c#

I have a method that serialize communication data. First the parameters is serialized into the main CallInformation object as string, then the CallInformation object will also be serialized and then later written to file :
_jSONSettings = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings();
_jSONSettings.DateTimeFormat = new DateTimeFormat("yyyy-MM-ddThh:mm:ss.fffZ");
_xmlWriterSettings = new System.Xml.XmlWriterSettings() { Indent = true };
var callInformation = logEvent.Properties.Values.First() as CallInformation;
DataContractJsonSerializer serializer;
if (TakeCharsInParameterObject > 0)
{
var counter = 0;
foreach (object param in callInformation.Parameters)
{
using (var stream = new MemoryStream())
{
serializer = new DataContractJsonSerializer(param.GetType(), _jSONSettings);
using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
{
serializer.WriteObject(xmlWriter, param);
stream.Flush();
stream.Position = SkipCharsInParameterObject;
using (var streamReader = new StreamReader(stream))
{
var buffer = new char[TakeCharsInParameterObject];
if (streamReader.Peek() >= 0)
{
streamReader.Read(buffer, 0, buffer.Length);
counter++;
callInformation.SerializedParameters += "{Parameter" + counter + ": " + new string(buffer) + "}";
}
}
}
}
}
}
var restult = "";
using (var stream = new MemoryStream())
{
serializer = new DataContractJsonSerializer(typeof(CallInformation), _jSONSettings);
using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
{
serializer.WriteObject(xmlWriter, callInformation);
stream.Flush();
stream.Position = 0;
using (var streamReader = new StreamReader(stream))
{
if (streamReader.Peek() >= 0)
restult = streamReader.ReadToEnd();
}
}
}
return restult;
The serialization for parameters works great but the second part do not. After stream.WriteObject the lengh of the stream and the position is still 0?
The CallInformation is a simple DataContract class that looks like this :
[DataContract]
public class CallInformation
{
public CallInformation()
{ }
[DataMember]
public string Address { get; set; }
[DataMember]
public Boolean IsEmpty { get; set; }
[DataMember]
public Boolean IsFaulted { get; set; } = false;
[DataMember]
public string Action { get; set; }
[DataMember]
public CallOrder CallDirection { get; set; }
public DateTime EventTime { get; set; } = DateTime.Now;
[DataMember]
public TimeSpan Duration { get; set; }
[DataMember]
public Boolean IsCallback { get; set; } = false;
[DataMember]
public string LogSource { get; set; } = "Unknown";
[DataMember]
public string SerializedParameters { get; set; } = "";
public List<object> Parameters { get; set; } = new List<object>();
[DataMember]
public string EventTimeDisplay
{
get { return EventTime.ToString("HH:mm:ss.fffffff"); }
set { }
}
}
Why is not the second serialization working?

XmlWriter has got internal buffer, you need to flush it to have stream position changed:
serializer.WriteObject(xmlWriter, callInformation);
xmlWriter.Flush();
stream.Position = 0;

Related

How can I return xml without <string> element?

I am using XML serialization in my project. It's working good but I am facing two issues.
Formatting of nodes
Remove <string></string> from response
Sample object which I am trying to serialize is :
[XmlRoot("result")]
public class SwRequestListModel
{
public SwRequestListModel()
{
SwRequest = new List<SwRequestShortInfo>();
}
[XmlElement("api_version")]
public string ApiVersion { get; set; }
[XmlElement("sw_request")]
public List<SwRequestShortInfo> SwRequest { get; set; }
}
[XmlRoot(ElementName = "sw_request_short_info")]
public class SwRequestShortInfo
{
[XmlElement(ElementName = "sw_ref_number")]
public string SwRefNumber { get; set; }
[XmlElement(ElementName = "db_no")]
public string DbNo { get; set; }
[XmlElement(ElementName = "engine_manufacturing_no")]
public string EngineManufacturingNo { get; set; }
[XmlElement(ElementName = "engine_ref_type")]
public string EngineRefType { get; set; }
[XmlElement(ElementName = "raised_date")]
public string RaisedDate { get; set; }
[XmlElement(ElementName = "raised_by")]
public string RaisedBy { get; set; }
[XmlElement(ElementName = "status")]
public string Status { get; set; }
[XmlElement(ElementName = "approved_by")]
public string ApprovedBy { get; set; }
[XmlElement(ElementName = "system_type")]
public string SystemType { get; set; }
}
The code which I am using to serialize is :
public static string ToXml(SwRequestListModel obj)
{
XmlSerializer s = new XmlSerializer(obj.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8,
NewLineOnAttributes = true,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
IndentChars = Environment.NewLine
};
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww, settings))
{
s.Serialize(writer, obj, ns);
return sww.ToString();
}
}
}
Response is display like :
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><result> <api_version>1.0</api_version> <sw_request> <sw_ref_number>588</sw_ref_number> <db_no>99899</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>test</system_type> </sw_request> <sw_request> <sw_ref_number>589</sw_ref_number> <db_no>88552</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>UNIC</system_type> </sw_request> <sw_request> <sw_ref_number>590</sw_ref_number> <db_no>33899</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>UNIC</system_type> </sw_request> </result></string>
Actual API Method is :
[HttpGet]
public string GetRequestList(string date, string status, string systemType)
{
SwRequestListModel result = new SwRequestListModel();
result.ApiVersion = "1.0";
//Here get data
return result.ToXml();
}
Browser result is :
Update :
I guess API method GetRequestList serialize string object. Try to return raw string.
[HttpGet]
public HttpResponseMessage GetRequestList(string date, string status, string systemType)
{
SwRequestListModel result = new SwRequestListModel();
result.ApiVersion = "1.0";
//Here get data
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(result.ToXml());
return response;
}

System.InvalidOperationException: 'There is an error in the XML document (1, 1).'

This error is showing up in my code, there is a second one that is as follows:
XmlException: The existing data at the root level is invalid. Line 1, position 1
I checked this second one saying there is a error with the file when there isn't any since I have 5 files inside my XMLFiles directory.
public static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(ImportSession));
MemoryStream stream = new MemoryStream();
using (StreamWriter sw = new StreamWriter(stream))
{
sw.Write(stream);
sw.Flush();
stream.Position = 0;
}
Console.ReadKey();
foreach (string filename in Directory.EnumerateFiles(#"C:\XMLFiles", "*.xml"))
{
ProcessFile(filename, stream, serializer);
}
void ProcessFile(string Filename, MemoryStream stream, XmlSerializer serializer)
{
bool temErro = false;
Console.WriteLine("A processar xml: " + Filename);
XmlDocument xml = new XmlDocument();
xml.Load(Filename);
ImportSession session = (ImportSession)serializer.Deserialize(stream);
foreach (Batch batch in session.Batches)
{
foreach (Document doc in batch.Documents)
{
foreach (Page page in doc.Pages)
{
if (!string.IsNullOrEmpty(batch.Processed.ToString()))
{
if (!string.IsNullOrEmpty(page.HasError.ToString()))
{
string Import = page.ImportFileName;
Console.WriteLine("Página com erro:" + Import);
temErro = true;
}
}
}
}
}
if (temErro)
Console.WriteLine("Ficheiro com erro: " + Filename);
else
Console.WriteLine("Ficheiro processado: " + Filename);
Console.WriteLine(Filename);
}
}
public class ImportSession
{
public Batch[] Batches { get; set; }
}
public class Batch
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Description { get; set; }
[XmlAttribute]
public string BatchClassName { get; set; }
[XmlAttribute]
public bool Processed { get; set; }
public Document[] Documents { get; set; }
}
public class Document
{
[XmlAttribute]
public string FormTypeName { get; set; }
public IndexField[] IndexFields { get; set; }
public Page[] Pages { get; set; }
}
public class IndexField
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Value { get; set; }
}
public class Page
{
[XmlAttribute]
public string ImportFileName { get; set; }
[XmlAttribute]
public string ErrorCode { get; set; }
[XmlAttribute]
public string ErrorMessage { get; set; }
[XmlIgnore]
public bool HasError => !string.IsNullOrWhiteSpace(ErrorMessage);
}
This app right now is only trying to read all the files and point out some parts that need to show up in the console and it was doing it but I was adviced on here to change into this object oriented and memory stream.
This:
MemoryStream stream = new MemoryStream();
using (StreamWriter sw = new StreamWriter(stream))
{
sw.Write(stream);
sw.Flush();
stream.Position = 0;
is basically meaningless. Whatever the contents of stream are meant to be: it isn't this. Ask yourself:
What is stream meant to contain?
At the moment it contains... itself, sort of, but not really?
If you intend the stream to be the file contents: just use File.OpenRead
I think this is based on a misunderstanding from answers to previous questions on the topic.
This should make it work. BUT keep in mind, that it is in no way production-ready.
public static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(ImportSession));
foreach (string filename in Directory.EnumerateFiles(#"C:\XMLFiles", "*.xml"))
{
ProcessFile(filename, serializer);
}
Console.ReadKey();
}
private static void ProcessFile(string Filename, XmlSerializer serializer)
{
bool temErro = false;
Console.WriteLine("A processar xml: " + Filename);
using (var file = File.OpenRead(Filename)) {
var session = (ImportSession)serializer.Deserialize(file);
// from here on the rest of your code ...
To minimize the code that keeps the file opened:
ImportSession session;
using (var file = File.OpenRead(Filename))
{
session = (ImportSession)serializer.Deserialize(file);
}
// file will be closed by disposal of FileStream using this notation
// rest of code
Addendum
if (!string.IsNullOrEmpty(batch.Processed.ToString()))
{ // Will ALWAYS be entered!
if (!string.IsNullOrEmpty(page.HasError.ToString()))
{ // Will ALWAYS be entered!
string Import = page.ImportFileName;
Console.WriteLine("Página com erro:" + Import);
temErro = true;
}
}
Let's look at it:
!string.IsNullOrEmpty(page.HasError.ToString()) is always true. Why?
page.HasError is of type bool. So, page.HasError.ToString() "Converts the value of this instance to its equivalent string representation (either "True" or "False")."
So, it will never be null or empty. So, string.IsNullOrEmpty will always be false, and !string.IsNullOrEmpty therefore always be true.
If you want to check the boolean value, you simply do if( page.HasError ) => "Page has an error"

How do I parse only value in json file

From this code. I want to parse only value from the json file
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
while (reader.Read())
{
if (reader.Value != null)
{
richTextBox1.Text = reader.Value.ToString();
}
else
{
MessageBox.Show("Error while parsing json file. Please try again.");
}
}
}
}
And the value is
{
"install.and": "a",
"install.emailAddress": "E-mailová adresa",
"install.emailIncorrect": "Zadejte platnou e-mailovou adresu.",
"install.emailRetryPrefix": "Neobdrželi jste e-mail? Zkuste to znovu",
"install.emailRetry": "Zkuste to znovu",
"install.emailSend": "Odeslat odkaz",
"install.emailSent": "E-mail byl odeslán!",
"install.emailSentTo": "E-mail byl odeslán",
"install.emailText1": "Můžete navštívit",
"install.emailText2": "Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.",
"install.installing": "Instalace...",
"install.later": "Instalovat později",
"install.licenseAgreement": "licenční smlouva",
"install.privacyPolicy": "zásady ochrany osobních údajů",
"install.quit": "Ukončit instalační program"
}
I want to parse it after : symbol. (Is it value?) to show in richTextbox as Text.
Try this code
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
var o = JObject.Load(reader);
foreach (var v in o)
{
var value = v.Value.Value<string>();
//do whatever you want with value
}
}
If you want only values joined by newline, then try this one
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
var o = JObject.Load(reader);
var e = o.Values().Select(x => x.Value<string>());
var values = string.Join(Environment.NewLine, e);
//do whatever you want with values
}
Introduce two temporary variable to hold key and value
string key = string.Empty;
string value = string.Empty;
Modify your while loop like this,
using (JsonTextReader reader = new JsonTextReader(file))
{
while (reader.Read())
{
if (reader.Value != null)
{
key = reader.Value.ToString();
if (reader.Read())
value = reader.Value.ToString();
Console.WriteLine("{0} : {1}", key,value);
//Instead of writing in a console, process and write it in Rich text box.
}
}
}
You can use Json.Net and create a model :
public class JsonObject
{
[JsonProperty("install.and")]
public string install_and { get; set; }
[JsonProperty("install.emailAddress")]
public string emailAddress { get; set; }
[JsonProperty("install.emailIncorrect")]
public string emailIncorrect { get; set; }
[JsonProperty("emailRetryPrefix")]
public string emailRetryPrefix { get; set; }
[JsonProperty("install.emailRetry")]
public string emailRetry { get; set; }
[JsonProperty("install.emailSend")]
public string emailSend { get; set; }
[JsonProperty("install.emailSent")]
public string emailSent { get; set; }
[JsonProperty("install.emailSentTo")]
public string emailSentTo { get; set; }
[JsonProperty("install.emailText1")]
public string emailText1 { get; set; }
[JsonProperty("install.emailText2")]
public string emailText2 { get; set; }
[JsonProperty("install.installing")]
public string installing { get; set; }
[JsonProperty("install.later")]
public string later { get; set; }
[JsonProperty("install.licenseAgreement")]
public string licenseAgreement { get; set; }
[JsonProperty("install.privacyPolicy")]
public string privacyPolicy { get; set; }
[JsonProperty("install.quit")]
public string quit { get; set; }
}
Then you can prase you json file:
string json_data = "{\"install.and\": \"a\",\"install.emailAddress\": \"E-mailová adresa\",\"install.emailIncorrect\": \"Zadejte platnou e-mailovou adresu.\",\"install.emailRetryPrefix\": \"Neobdrželi jste e-mail? Zkuste to znovu\",\"install.emailRetry\": \"Zkuste to znovu\",\"install.emailSend\": \"Odeslat odkaz\",\"install.emailSent\": \"E-mail byl odeslán!\",\"install.emailSentTo\": \"E-mail byl odeslán\",\"install.emailText1\": \"Můžete navštívit\",\"install.emailText2\": \"Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.\",\"install.installing\": \"Instalace...\",\"install.later\": \"Instalovat později\",\"install.licenseAgreement\": \"licenční smlouva\",\"install.privacyPolicy\": \"zásady ochrany osobních údajů\",\"install.quit\": \"Ukončit instalační program\"";
JsonObject data = JsonConvert.DeserializeObject<JsonObject>(json_data);
richTextBox1.Text = data.emailAddress;
richTextBox2.Text = data.emailIncorrect;
richTextBox3.Text = data.emailRetry;
[...]
First, Install newtonsoft.json from nuget package manager. Add the namespace
using Newtonsoft.Json.Linq;
create a class to easily handle the values.
class Details
{
public string and;
public string EmailAddress;
public string EmailIncorrect;
public string EmailRetry;
public string EmailSend;
public string EmailSent;
}
Then read the file into a string and parse it using JObject.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = File.ReadAllText(openFileDialog1.FileName);
JObject jo = JObject.Parse(file);
Details dt = new Details();
dt.and = (string)jo["install.and"];
richTextBox1.Text = reader.Value.ToString();
}

How to use protobuf-net for xml-serialization?

Maybe i misunderstood something for serialization. i wanna serialize my .net object fastest way. i made some googling i found protobuf. Myfirstquestion is ProtoBuf.Net has avility for xml serailization.if it has, can i use it for xml serialization.
My model:
[XmlType]
public class CT {
[XmlElement(Order = 1)]
public int Foo { get; set; }
}
[XmlType]
public class TE {
[XmlElement(Order = 1)]
public int Bar { get; set; }
}
[XmlType]
public class TD {
[XmlElement(Order = 1)]
public List CTs { get; set; }
[XmlElement(Order = 2)]
public List TEs { get; set; }
[XmlElement(Order = 3)]
public string Code { get; set; }
[XmlElement(Order = 4)]
public string Message { get; set; }
[XmlElement(Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(Order = 6)]
public DateTime EndDate { get; set; }
}
my serializer :
CT ct = new CT() { Foo = 1 };
List<CT> list = new List<CT>();
list.Add(ct);
TE te = new TE() { Bar = 2 };
List<TE> list2 = new List<TE>();
list2.Add(te);
TD td = new TD() { Code = "Test",Message = "Yusuf",StartDate = DateTime.Now,EndDate = DateTime.Now,CTs = list,TEs = list2 };
List<TD> list3 = new List<TD>();
list3.Add(td);
Stopwatch stopwatch5 = new Stopwatch();
stopwatch5.Start();
string str = String.Empty;
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(list3);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data); <--SHOULD CREATE XML
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
stopwatch5.Stop();
Console.WriteLine(((double)(stopwatch5.Elapsed.TotalMilliseconds * 1000000) / 1000000).ToString("0.00 ns"));
Console.Read();
}
public static byte[] Serialize(List<TD> tData) {
using(var ms = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(ms,tData);
return ms.ToArray();
}
}
public static List<TD> Deserialize(byte[] tData) {
using(var ms = new MemoryStream(tData)) {
return ProtoBuf.Serializer.Deserialize<List<TD>>(ms);
}
}
it should create xml as a result of " string xml = Encoding.UTF8.GetString(data);". But doesn't. How can i produxe xml result?
Protocol buffers doesn't serialize objects to XML.
It produces binary data. And it has its own set of attributes.
Check this answer
Is Protobuf-net's serialization/deserialization faster than XML ? Yes, by far.
However XmlSerializer is fast enough for most of the tasks.
What you should remind when using it though, is:
XmlSerializer instance is generating code on the fly and compile this code into an assembly.
This generated assembly is then used to serialize and deserialize your objects really fast.
So you should cache instances of XmlSerializer (and avoid recreating them)
you could add a warm up by calling Serialize and Deserialize in order to initialize inner objects and jit them.
You could even go further by generating the auto-generated assembly by yourself, but then you should remember to regenerate each time you change the objects (It can be automated with a MsBuild Task).
You can also look further optimizations:
On Stack Overflow
With Sgen
You can only have one tag at the root level of your xml. So either TD cannot be a list, or you must have an outer tag around the List. This code works
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TD td = new TD()
{
Code = "Test",
Message = "Yusuf",
StartDate = DateTime.Now,
EndDate = DateTime.Now,
CTs = new List<CT>() {
new CT() { Foo = 1},
new CT() { Foo = 2},
new CT() { Foo = 3},
},
TEs = new List<TE>() {
new TE() { Bar = 1},
new TE() { Bar = 2},
new TE() { Bar = 3},
}
};
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(td);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data);
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
}
public static byte[] Serialize(TD tData)
{
using (var ms = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(TD));
serializer.Serialize(ms, tData);
return ms.ToArray();
}
}
public static TD Deserialize(byte[] tData)
{
using (var ms = new MemoryStream(tData))
{
XmlSerializer xs = new XmlSerializer(typeof(TD));
return (TD)xs.Deserialize(ms);
}
}
}
[XmlRoot("CT")]
public class CT
{
[XmlElement(ElementName = "Foo", Order = 1)]
public int Foo { get; set; }
}
[XmlRoot("TE")]
public class TE
{
[XmlElement(ElementName = "Bar", Order = 1)]
public int Bar { get; set; }
}
[XmlRoot("TD")]
public class TD
{
[XmlElement(ElementName = "CTs", Order = 1)]
public List<CT> CTs { get; set; }
[XmlElement(ElementName = "TEs", Order = 2)]
public List<TE> TEs { get; set; }
[XmlElement(ElementName = "Code", Order = 3)]
public string Code { get; set; }
[XmlElement(ElementName = "Message", Order = 4)]
public string Message { get; set; }
[XmlElement(ElementName = "StartDate", Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(ElementName = "EndDate", Order = 6)]
public DateTime EndDate { get; set; }
}
}
​

Unable to serialize object into XML file

I'm trying to save a dictionary of Matrix into an Xml file.
My Matrix class attributes are :
public class Matrix
{
public int Lines { get; set; }
public int Columns { get; set; }
public double[,] Elements { get; set; }
public string name { get; set; }
}
After many attempts, I wrote this :
string fileName = dlg.FileName;
Stream writer = new FileStream(fileName,FileMode.Create);
foreach (KeyValuePair<String, Matrix> matrice in CalMat.Calculatrice.listMatrix)
{
XmlSerializer x = new XmlSerializer(matrice.GetType());
x.Serialize(writer, matrice);
}
writer.Close();
If i run this code with one matrix, the file is created, but i only have this sentence written :
<?xml version="1.0"?><KeyValuePairOfStringMatrix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0"?>
I think my code is missing something but I don't know what. A write method, I guess.
Thank you for your time!
I don't think the default KeyValuePair is serializable,
try building your own KeyValuePair class:
[Serializable]
[XmlType(TypeName="MyTypeName")]
public struct KeyValuePair<T1, T2>
{
public T1 Key { get; set; }
public T2 Value { get; set; }
}
Using BinaryFormatter this is the code:
[Serializable] // mark with Serializable
public class Matrix
{
public Matrix(string name, int lines, int columns)
{
Name = name;
Lines = lines;
Columns = columns;
Elements = new double[Lines, Columns];
}
public int Lines { get; set; }
public int Columns { get; set; }
public double[,] Elements { get; set; }
public string Name { get; set; }
}
public static void Main()
{
var path = #"D:\serialize.data"; // use the path that you want
// this is an example collection
var listMatrix = new Dictionary<string, Matrix>();
listMatrix.Add("matrix_1", new Matrix("Matrix 1", 1, 2));
listMatrix.Add("matrix_2", new Matrix("Matrix 2", 2, 2));
// Serialization
var stream = new FileStream(path, FileMode.Create);
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, listMatrix);
stream.Close();
// Deserialization
stream = new FileStream(path, FileMode.Open);
var result = (Dictionary<string, Matrix>)binaryFormatter.Deserialize(stream);
stream.Close();
}
Using XmlSerializer this is the code:
// I implement my custom KeyValuePair to serialize (because XmlSerializer can not serialize the .net KeyValuePair)
public struct CustomKeyValuePair<T1, T2>
{
public CustomKeyValuePair(T1 key, T2 value): this()
{
Key = key;
Value = value;
}
public T1 Key { get; set; }
public T2 Value { get; set; }
// here I specify how is the cast
public static explicit operator CustomKeyValuePair<T1, T2>(KeyValuePair<T1, T2> keyValuePair)
{
return new CustomKeyValuePair<T1, T2>(keyValuePair.Key, keyValuePair.Value);
}
}
// Matrix class used to Serialize with XmlSerailzer
public class Matrix
{
public Matrix() { } // need a default constructor
public Matrix(string name, int lines, int columns)
{
Name = name;
Lines = lines;
Columns = columns;
Elements = new double[Columns][];
for (int i = 0; i < Elements.Length; i++)
{
Elements[i] = new double[Columns];
}
}
public int Lines { get; set; }
public int Columns { get; set; }
public double[][] Elements { get; set; } // I use double[][] because XmlSerialzer can not serialize a two-dimensional array (double[,])
public string Name { get; set; }
}
public static void Main()
{
var path = #"D:\serialize.data"; // use the path that you want
// this is an example collection
var listMatrix = new Dictionary<string, Matrix>();
listMatrix.Add("matrix_1", new Matrix("Matrix 1", 1, 2));
listMatrix.Add("matrix_2", new Matrix("Matrix 2", 2, 2));
// Serialization
var stream = new FileStream(path, FileMode.Create);
var xmlSerializer = new XmlSerializer(typeof(CustomKeyValuePair<string, Matrix>[]));
var aux = listMatrix.Select(keyValuePair => (CustomKeyValuePair<string, Matrix>) keyValuePair).ToArray();
xmlSerializer.Serialize(stream, aux); // I serialize an array to make easy the deserailizer
stream.Close();
// Deserialization
stream = new FileStream(path, FileMode.Open);
var result = (CustomKeyValuePair<string, Matrix>[])xmlSerializer.Deserialize(stream);
stream.Close();
}

Categories