XML data not being loaded into c# class - c#

I am trying to load data from an XML file into a c# class but am not getting data being loaded in Notifications. The rest of the class (not shown) is correctly populated so I am assuming that my class definition is incorrect. Can anyone shed any light on this?
public partial class ISTimetables
{
[XmlElement]
public List<ISNotification> Notifications { get; set; }
}
[Serializable()]
public partial class ISNotification
{
public ISNotification()
{
On = new List<ISProcessStep>();
Notify = new List<ISNotify>();
}
[XmlElement]
public List<ISProcessStep> On { get; set; }
[XmElement]
public List<ISNotify> Notify { get; set; }
}
[Serializable()]
public partial class ISNotify
{
public string Email { get; set; }
public string SimpleEmail { get; set; }
public string SMS { get; set; }
}
[Serializable()]
public enum ISProcessStep
{
[XmlEnum("Calculated")]
Calculated,
[XmlEnum("Reported")]
Reported,
[XmlEnum("Customer Approved")]
CustomerApproved,
[XmlEnum("Rejected")]
Rejected
}
The data I am trying to load is as follows:
<Notifications>
<Notification>
<On>Calculated</On>
<On>Reported</On>
<Notify SimpleEmail="me#company.com"/>
<Notify Email="you#company.com"/>
<Notify SMS="0123456789"/>
</Notification>
<Notification>
<On>Customer Approved</On>
<Notify Email="him#company.com"/>
</Notification>
</Notifications>

Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication21
{
class Program
{
const string FILEName = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(ISTimetables));
XmlTextReader reader = new XmlTextReader(FILEName);
ISTimetables tables = (ISTimetables)serializer.Deserialize(reader);
}
}
[XmlRoot("Notifications")]
public partial class ISTimetables
{
[XmlElement("Notification")]
public List<ISNotification> Notifications { get; set; }
}
[XmlRoot("Notification")]
public partial class ISNotification
{
public ISNotification()
{
On = new List<ISProcessStep>();
Notify = new List<ISNotify>();
}
[XmlElement]
public List<ISProcessStep> On { get; set; }
[XmlElement]
public List<ISNotify> Notify { get; set; }
}
[Serializable()]
public partial class ISNotify
{
public string Email { get; set; }
public string SimpleEmail { get; set; }
public string SMS { get; set; }
}
[Serializable()]
public enum ISProcessStep
{
[XmlEnum("Calculated")]
Calculated,
[XmlEnum("Reported")]
Reported,
[XmlEnum("Customer Approved")]
CustomerApproved,
[XmlEnum("Rejected")]
Rejected
}
}

Related

C# JsonSerializer.Deserialize array

All,
Edit: Firstly thanks for everyone's help. Secondly I'm new to Stack Overflow so apologises if I've added this edit incorrectly.
Following the commments and replies I've updated my class structure to:
services class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace RTT_API
{
class services
{
public List<service> service = new List<service>();
public services()
{
}
}
}
Service class:
using System;
using System.Collections.Generic;
using System.Text;
namespace RTT_API
{
class service
{
public string atocCode{get; set;}
public service()
{
}
}
}
Unfortunately I'm still getting the same error. I think I still haven't quite matched my class structure to the JSON structure? Unfortunately I'm not sure where my mistake is. If it helps to highlight my mistake using a comparison then the following works:
Location class
using System;
using System.Collections.Generic;
using System.Text;
namespace RTT_API
{
class location
{
public string name { get; set; }
public string crs { get; set; }
public location()
{
}
}
}
Location deserilisation command and test output:
location locations = JsonSerializer.Deserialize<location>(channelResponse.RootElement.GetProperty("location").GetRawText());
MessageBox.Show(locations.crs);
Original question:
My JSON is as follows:
{
"location": {
"name": "Bournemouth",
"crs": "BMH",
"tiploc": "BOMO"
},
"filter": null,
"services": [
{
"locationDetail": {
"realtimeActivated": true,
"tiploc": "BOMO",
"crs": "BMH",
"description": "Bournemouth",
"wttBookedArrival": "011630",
"wttBookedDeparture": "011830",
"gbttBookedArrival": "0117",
"gbttBookedDeparture": "0118",
"origin": [
{
"tiploc": "WATRLMN",
"description": "London Waterloo",
"workingTime": "230500",
"publicTime": "2305"
}
],
"destination": [
{
"tiploc": "POOLE",
"description": "Poole",
"workingTime": "013000",
"publicTime": "0130"
}
],
"isCall": true,
"isPublicCall": true,
"realtimeArrival": "0114",
"realtimeArrivalActual": false,
"realtimeDeparture": "0118",
"realtimeDepartureActual": false,
"platform": "3",
"platformConfirmed": false,
"platformChanged": false,
"displayAs": "CALL"
},
"serviceUid": "W90091",
"runDate": "2013-06-11",
"trainIdentity": "1B77",
"runningIdentity": "1B77",
"atocCode": "SW",
"atocName": "South West Trains",
"serviceType": "train",
"isPassenger": true
}
]
}
My class structure is as follows:
servicelist class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace RTT_API
{
class servicelist
{
public List<services> service = new List<services>();
public servicelist()
{
}
}
}
services class:
using System;
using System.Collections.Generic;
using System.Text;
namespace RTT_API
{
class services
{
public int serviceUid;
public services()
{
}
}
}
For deserialisation I have tried:
services servicelist = JsonSerializer.Deserialize<services>(channelResponse.RootElement.GetProperty("services").GetRawText());
and
servicelist servicelist = JsonSerializer.Deserialize<servicelist>(channelResponse.RootElement.GetProperty("services").GetRawText());;
In both cases I get 'System.Text.Json.JsonException'
I think there is a mismatch betwee the class structure and the JSON but I can't work what the problem is? It's the first time I've tried to desarialise an array.
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace RTT_API
{
class location
{
public string name { get; set; }
public string crs { get; set; }
public location()
{
}
}
}
You can generate exact C# classes according to your JSON using tools for exactly that purpose. I used https://json2csharp.com/ , another is https://jsonutils.com/ - these are web services and don't require installation on computer, another option is generating classes through Visual Studio (with Web Essentials installed), there you would use Edit - Paste special - paste JSON as class.
Once you have the valid classes (I pasted generated classes below) you can deserialize entire Root object and then access any part of it, including services part:
// jsonInputText holds entire JSON string you posted
Root root = JsonSerializer.Deserialize<Root>(jsonInputText);
List<Service> serviceList = root.services;
Generated classes:
public class Location
{
public string name { get; set; }
public string crs { get; set; }
public string tiploc { get; set; }
}
public class Origin
{
public string tiploc { get; set; }
public string description { get; set; }
public string workingTime { get; set; }
public string publicTime { get; set; }
}
public class Destination
{
public string tiploc { get; set; }
public string description { get; set; }
public string workingTime { get; set; }
public string publicTime { get; set; }
}
public class LocationDetail
{
public bool realtimeActivated { get; set; }
public string tiploc { get; set; }
public string crs { get; set; }
public string description { get; set; }
public string wttBookedArrival { get; set; }
public string wttBookedDeparture { get; set; }
public string gbttBookedArrival { get; set; }
public string gbttBookedDeparture { get; set; }
public List<Origin> origin { get; set; }
public List<Destination> destination { get; set; }
public bool isCall { get; set; }
public bool isPublicCall { get; set; }
public string realtimeArrival { get; set; }
public bool realtimeArrivalActual { get; set; }
public string realtimeDeparture { get; set; }
public bool realtimeDepartureActual { get; set; }
public string platform { get; set; }
public bool platformConfirmed { get; set; }
public bool platformChanged { get; set; }
public string displayAs { get; set; }
}
public class Service
{
public LocationDetail locationDetail { get; set; }
public string serviceUid { get; set; }
public string runDate { get; set; }
public string trainIdentity { get; set; }
public string runningIdentity { get; set; }
public string atocCode { get; set; }
public string atocName { get; set; }
public string serviceType { get; set; }
public bool isPassenger { get; set; }
}
public class Root
{
public Location location { get; set; }
public object filter { get; set; }
public List<Service> services { get; set; }
}
If you need to deserialize only just a part of your json then you can use the JObject and JToken helper classes for that.
var json = File.ReadAllText("Sample.json");
JObject topLevelObject = JObject.Parse(json);
JToken servicesToken = topLevelObject["services"];
var services = servicesToken.ToObject<List<Service>>();
The topLevelObject contains the whole json in a semi-parsed format.
You can use the indexer operator to retrieve an object / array by using one of the top level keys.
On a JToken you can call the ToObject<T> to deserialize the data into a custom data class.
In order to be able to parse your json I had to adjust the services type because the W90091 as serviceUid can't be parsed as int. So here is my Service class definition:
public class Service
{
public string ServiceUid;
}
One thing to note here is that casing does not matter in this case so please use CamelCasing in your domain models as you would normally do in C#.
Thanks for everyone's help.
Firstly I had to make a few changes to the class names as they didn't match the JSON. I also had to change the syntax of two commands which I've detailed below:
I changed the definition of the list of objects from:
public List<services> service = new List<services>();
to:
public List<service> destination { get; set; };
and deserilisation command from:
services servicelist = JsonSerializer.Deserialize<services>(channelResponse.RootElement.GetProperty("services").GetRawText());
to
var servicelist = JsonSerializer.Deserialize<List<service>>(channelResponse.RootElement.GetProperty("services").GetRawText());
The change from services to var might not be the best solution. I think it's the first change, and matching the class names to the JSON, that fundamentally fixed the issue.

There is an error in XML document (2, 2) when desearlizing xml to c# object

XML
<?xml version="1.0" encoding="UTF-8"?>
<orgc:Organizations xmlns:orgc="urn:workday.com/connector/orgs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<orgc:Organization>
<orgc:Organization_ID>SR1Code34</orgc:Organization_ID>
<orgc:Organization_Code>SR1Code34</orgc:Organization_Code>
<orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
<orgc:Organization_Name>LTL Services</orgc:Organization_Name>
<orgc:Organization_Description>LTL Services</orgc:Organization_Description>
<orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
<orgc:Inactive>false</orgc:Inactive>
<orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
</orgc:Organization>
<orgc:Organization>
<orgc:Organization_ID>SR1Code35</orgc:Organization_ID>
<orgc:Organization_Code>SR1Code35</orgc:Organization_Code>
<orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
<orgc:Organization_Name>Consolidation</orgc:Organization_Name>
<orgc:Organization_Description>Consolidation</orgc:Organization_Description>
<orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
<orgc:Inactive>false</orgc:Inactive>
<orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
</orgc:Organization>
</orgc:Organizations>
Class
[XmlRoot(ElementName = "Organizations", Namespace = "urn: workday.com/connector/orgs", IsNullable = true )]
public class CostCenterHierarchy
{
[XmlElement("orgc:Organization_ID")]
public string CostCenterHierarchyId { get; set; }
[XmlElement("orgc:Organization_Code")]
public string Code { get; set; }
[XmlElement("orgc:Organization_Name")]
public string Name { get; set; }
[XmlElement("orgc:Organization_Description")]
public string Description { get; set; }
[XmlElement("orgc:Organization_Subtype")]
public string Subtype { get; set; }
[XmlElement("orgc:Superior_Organization")]
public string ParentHierarchyId { get; set; }
}
Method to deseralize xml to c# class
private List<CostCenterHierarchy> ProcessCostCenterHierarchy(string filePath)
{
var costCenterHierarchyList = new List<CostCenterHierarchy>();
//var costCenterHierarchy = new CostCenterHierarchy();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<CostCenterHierarchy>));
using (var reader = XmlReader.Create(filePath))
{
var test = xmlSerializer.Deserialize(reader);
}
return costCenterHierarchyList;
}
Error Message
Message = "There is an error in XML document (2, 2)."
InnerException = {"<Organizations xmlns='urn:workday.com/connector/orgs'> was not expected."}
I'm not sure where I am going wrong. Seems like it should be pretty easy but I've played around with this and keep getting the same error message. Any help would be much appreciated.
The code below works. You have an array and serialization doesn't like a list as the type. The Url "urn:workday.com/connector/orgs" the serializaer doesn't like and had to replace the "urn:" with "http://workday.com/connector/orgs".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Organizations));
Organizations organizations = (Organizations)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Organizations", Namespace = "http://workday.com/connector/orgs")]
public class Organizations
{
[XmlElement(ElementName = "Organization", Namespace = "http://workday.com/connector/orgs")]
public List<CostCenterHierarchy> organizations { get; set; }
}
public class CostCenterHierarchy
{
[XmlElement("Organization_ID")]
public string CostCenterHierarchyId { get; set; }
[XmlElement("Organization_Code")]
public string Code { get; set; }
[XmlElement("Organization_Name")]
public string Name { get; set; }
[XmlElement("Organization_Description")]
public string Description { get; set; }
[XmlElement("Organization_Subtype")]
public string Subtype { get; set; }
[XmlElement("Superior_Organization")]
public string ParentHierarchyId { get; set; }
}
}

How to deserialize a JSON with array of strings using "System.Runtime.Serialization.Json" class in C#?

I am working with C# project in which, most data was of basic type all these days such as string, int, bool. Our client imprlements the JSON with System.Runtime.Serialization.Json, where we deserialize JSON sent from a server that is implemented in C++.
So for instance if I had to De-serialize a JSON sent from the server with 2 string keys such as:
{
"key1":"value1",
"key2":"value2"
}
we would define a class such as
[DataContract]
public class DeserializeKeys
{
[DataMember] public string key1 { get; set; }
[DataMember] public string key2 { get; set; }
};
Our server side code has changed to now send array of strings as a value as shown in the JSON object below:
{
"key1":"value1",
"key2":
[
"arrayValue1",
"arrayValue2",
"arrayValue3"
]
}
Please help me write a corresponding class that can deserialize the given JSON using "System.Runtime.Serialization.Json" class in C#.
I have already tried:
[DataContract]
public class DeserializeKeys
{
[DataMember] public string key1 { get; set; }
[DataMember] public string[] key2 { get; set; }
};
and
[DataContract]
public class DeserializeKeys
{
[DataMember] public string key1 { get; set; }
[DataMember] public List<string> key2 { get; set; }
};
but I am getting null for key2 upon deserialization.
What is the right way to define a class so that the JSON deserialization of array of string happens just as it works currently for a single string.
Seems to work correctly for me (both string[] and List<string> ) i'll assume that you deserialized it in a wrong way. Here is a minimal example that should get your started on fixing your app.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Serializator
{
public class Serializator
{
static public SomeClass ReadToObject(string json)
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(SomeClass));
var deserialized = ser.ReadObject(ms) as SomeClass;
ms.Close();
return deserialized;
}
}
[DataContract]
public class SomeClass
{
[DataMember] public string key1 { get; set; }
[DataMember] public List<string> key2 { get; set; }
};
class Program
{
static void Main(string[] args)
{
SomeClass sc = Serializator.ReadToObject("{\"key1\":\"value1\", \"key2\":[\"arrayValue1\", \"arrayValue2\", \"arrayValue3\"]}");
foreach(var item in sc.key2)
{
Console.WriteLine(item);
}
}
}
}
As it seems the issue lay in a data format not serialization mechanizm (the values of keys were also serialized objects) here is an updated version for the particular format the application required.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Serializator
{
public class Serializator
{
static public Object ReadToObject(string json, Type t)
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(t);
var deserialized = ser.ReadObject(ms);
ms.Close();
return deserialized;
}
}
[DataContract]
public class IntermediateClass
{
[DataMember] public string error { get; set; }
[DataMember] public List<string> group { get; set; }
};
[DataContract]
public class ErrorClass
{
[DataMember] public string ErrorCode { get; set; }
[DataMember] public string ErrorMessage { get; set; }
};
public class GroupClass
{
[DataMember] public int ID { get; set; }
[DataMember] public string Name { get; set; }
}
public class CombinedClass
{
public ErrorClass error { get; set; }
public List<GroupClass> group { get; set; }
}
class Program
{
static void Main(string[] args)
{
CombinedClass cb = new CombinedClass();
IntermediateClass ic = (IntermediateClass)Serializator.ReadToObject("{\"error\":\"{\\n \\\"ErrorCode\\\" : 0,\\n \\\"ErrorMessage\\\" : \\\"Success.\\\"\\n}\\n\",\"group\":[\"{\\n \\\"ID\\\" : 1,\\n \\\"Name\\\" : \\\"Student1\\\"\\n}\\n\",\"{\\n \\\"ID\\\" : 2,\\n \\\"Name\\\" : \\\"Student2\\\"\\n}\\n\"]}", typeof(IntermediateClass));
cb.group = new List<GroupClass>();
foreach (var item in ic.group)
{
cb.group.Add((GroupClass)Serializator.ReadToObject(item, typeof(GroupClass)));
}
cb.error = (ErrorClass)Serializator.ReadToObject(ic.error, typeof(ErrorClass));
Console.WriteLine(cb.error.ErrorCode);
Console.WriteLine(cb.error.ErrorMessage);
Console.WriteLine(cb.group[0].Name);
Console.WriteLine(cb.group[0].ID);
Console.WriteLine(cb.group[1].Name);
Console.WriteLine(cb.group[1].ID);
}
}
}

What is wrong in my class hierarchy for an xml deserialization?

I'm trying to deserialize the following xml document into a C# object:
<ns1:StockerFichiers
xmlns:ns1="http://www.foo.fr/bar/Repository"
xmlns:ns0="http://www.foo.fr/bar/Transport/">
<ns1:fichiersAStocker>
<ns0:FichierIdentifie>
<ns0:Contenu></ns0:Contenu>
<ns0:DomaineIdLocalDoc>128</ns0:DomaineIdLocalDoc>
<ns0:EstOriginal>true</ns0:EstOriginal>
<ns0:IdLocalDoc>2018-07-06T154554_70183_2</ns0:IdLocalDoc>
<ns0:PieceDynamique>false</ns0:PieceDynamique>
<ns0:GoldenSource>false</ns0:GoldenSource>
<ns0:TypeDoc>PDF</ns0:TypeDoc>
<ns0:TypeMime>application/pdf</ns0:TypeMime>
</ns0:FichierIdentifie>
</ns1:fichiersAStocker>
</ns1:StockerFichiers>
I know a lot of deserialization questions already exist, but even if some seems to be solving the same issue I face, None of what I've tried did populate my List<FichierIdentifie>.
Where I deserialize:
public void StockerFichiersXmlBase64(string fichiersAStocker)
{
//serializer
XmlRootAttribute xroot = new XmlRootAttribute();
xroot.ElementName = "StockerFichiers";
xroot.Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY; //ns1
XmlSerializer deserializer = new XmlSerializer(typeof(StockerFichiersRoot),xroot );
//fichiersAStocker is base64 encoded
byte[] data = Convert.FromBase64String(fichiersAStocker);
StringReader stringReader = new StringReader(Encoding.UTF8.GetString(data));
//deserialization
StockerFichiersRoot deserializedFiles = (StockerFichiersRoot)deserializer.Deserialize(stringReader);
}
My current version :
// Root
[XmlRoot(ElementName = "StockerFichiers", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
public class StockerFichiersRoot
{
[XmlElement(ElementName = "fichiersAStocker", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
public FichiersAStocker fichiersAStocker { get; set; }
}
//sub root
public class FichiersAStocker
{
[XmlArray(ElementName = "fichiersAStocker", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
[XmlArrayItem(ElementName = "FichierIdentifie", Namespace=NamespacesConstantes.NAMESPACE_MSS_TRANSPORT)]
public List<FichierIdentifie> FichiersIdentifie { get; set; }
}
public class FichierIdentifie
{
[XmlElement(Namespace = NamespacesConstantes.NAMESPACE_TRANSPORT)]
public byte[] Contenu { get; set; }
//all fields are similar to the first one
}
And with this variation of the subroot class according to Is it possible to deserialize XML into List<T>? :
//sub root
public class FichiersAStocker
{
[XmlElement(ElementName = "FichierIdentifie", Namespace=NamespacesConstantes.NAMESPACE_MSS_TRANSPORT)]
public List<FichierIdentifie> FichiersIdentifie { get; set; }
}
I've also tried to remove the class FichiersAStocker (the sub root), to put the List<FichierIdentifie> in the root class, with both [xmlArray..] and [XmlElement] variations but with no success.
I always get an object with the list empty.
Try using XML2CSharp to generate class. Then try using that class or use it for debugging.
Generated code for your XML looks like this:
(You can remove unwanted properties)
/*
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="FichierIdentifie", Namespace="http://www.foo.fr/bar/Transport/")]
public class FichierIdentifie {
[XmlElement(ElementName="Contenu", Namespace="http://www.foo.fr/bar/Transport/")]
public string Contenu { get; set; }
[XmlElement(ElementName="DomaineIdLocalDoc", Namespace="http://www.foo.fr/bar/Transport/")]
public string DomaineIdLocalDoc { get; set; }
[XmlElement(ElementName="EstOriginal", Namespace="http://www.foo.fr/bar/Transport/")]
public string EstOriginal { get; set; }
[XmlElement(ElementName="IdLocalDoc", Namespace="http://www.foo.fr/bar/Transport/")]
public string IdLocalDoc { get; set; }
[XmlElement(ElementName="PieceDynamique", Namespace="http://www.foo.fr/bar/Transport/")]
public string PieceDynamique { get; set; }
[XmlElement(ElementName="SisraGoldenSource", Namespace="http://www.foo.fr/bar/Transport/")]
public string SisraGoldenSource { get; set; }
[XmlElement(ElementName="TypeDocSisra", Namespace="http://www.foo.fr/bar/Transport/")]
public string TypeDocSisra { get; set; }
[XmlElement(ElementName="TypeMime", Namespace="http://www.foo.fr/bar/Transport/")]
public string TypeMime { get; set; }
}
[XmlRoot(ElementName="fichiersAStocker", Namespace="http://www.foo.fr/bar/Repository")]
public class FichiersAStocker {
[XmlElement(ElementName="FichierIdentifie", Namespace="http://www.foo.fr/bar/Transport/")]
public FichierIdentifie FichierIdentifie { get; set; }
}
[XmlRoot(ElementName="StockerFichiers", Namespace="http://www.foo.fr/bar/Repository")]
public class StockerFichiers {
[XmlElement(ElementName="fichiersAStocker", Namespace="http://www.foo.fr/bar/Repository")]
public FichiersAStocker FichiersAStocker { get; set; }
[XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns1 { get; set; }
[XmlAttribute(AttributeName="ns0", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns0 { get; set; }
}
}
Really frustrating mistake that took me half a day to solve :
notice how "NamespacesConstantes.NAMESPACE_MSS_TRANSPORT" is close to "NamespacesConstantes.NAMESPACE_TRANSPORT". Add some lazy autocompletion and you can fool yourself while defining the [XmlElement...] in the "FichiersAStocker" class.
Thanks for your help Matt, I noticed this mistake while i paste some of my code on https://dotnetfiddle.net/ ! :)

Deserialize a class with list of class containing a list: Error: "the collection is read-only"

I'd like to serialize a class construct that contains a list of up to 16 sensors ( and additional a list with up to 4 PressureSensors), which contain a list with the coefficients of their function (and some more information like ID ...).
The PressureSensor thing is additional and just shows that there are some more collections in the module. Otherwise a
List<List<SimpleSensor>>
would have been sufficient.
The XML is created without problems, only deserialization fails
<?xml version="1.0" encoding="utf-8"?>
<Module xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SingleSensors>
<SSensor>
<ID>Sensor1</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</SSensor>
<SSensor>
<ID>Sensor2</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</SSensor>
</SingleSensors>
<PressureSensors>
<PSensor>
<ID>Pressure1</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</PSensor>
<PSensor>
<ID>Pressure2</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</PSensor>
</PressureSensors>
</Module>
The Class construct is as follows:
[Serializable]
public class SimpleSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlArray("Function")]
public Collection<double> Coefficient { get; set; }
//## Constructor:
public SimpleSensor()
{
Id = "00";
double[] content = new double[2] { 0, 1 };
Coefficient = new Collection<double>(content);
}
}
[Serializable]
[XmlRoot("Module")]
public class MostModule
{
[XmlArray("SingleSensors")]
[XmlArrayItem("SSensor")]
public List<SimpleSensor> Sensor { get; set; }
[XmlArray("PressureSensors")]
[XmlArrayItem("PSensor")]
public List<SimpleSensor> PressureSensor { get; set; }
//## Constructor:
public MostModule()
{
//Initialise SimpleSensors with 2 Sensors
SimpleSensor[] SensorArray = new SimpleSensor[2];
for (int i = 0; i < 2; i++)
{
SensorArray[i] = new SimpleSensor();
SensorArray[i].Id = "Sensor" + (i + 1);
}
Sensor = new List<SimpleSensor>(SensorArray);
PressureSensor = new List<SimpleSensor>(SensorArray);
}
public static MostModule Deserialize(string fileName)
{
MostModule Sensors;
XmlSerializer myXMLSerial = new XmlSerializer(typeof(MostModule));
using (StreamReader sr = File.OpenText(fileName))
{
Sensors = (MostModule)myXMLSerial.Deserialize(sr);
}
return Sensors;
}
When I tried to Serialize this class it works properly, but the deserialization stops with an error and the inner exception:
NotSupportedException "The collection is read-only."
I have tried the same with hard defined sensors and there is no problem in Serializing and deserializing:
public class MostModule
{
public SimpleSensor Sensor1 { get; set; }
public SimpleSensor Sensor2 { get; set; }
Is there a way to serialize the module this way, or do I have to program it all with hard defined Variables that i have to put into a list from the main?
Or is there any other suggestion for saving and loading my data in this kind of construction?
I made the following List<> objects
[XmlRoot("SingleSensors")]
public class SimpleSensor
{
[XmlElement("SSensor")]
public List<SSensor> sSensor { get; set; }
}
[XmlRoot("PressureSensors")]
public class PressureSensors
{
[XmlElement("PSensor")]
public List<SSensor> sSensor { get; set; }
}
Define SimpleSensor.Coefficient as a plain array and it will work:
[Serializable]
public class SimpleSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlArray("Function")]
public double[] Coefficient { get; set; }
//## Constructor:
public SimpleSensor()
{
Id = "00";
double[] content = new double[2] { 0, 1 };
Coefficient = content;
}
}
Try this
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication27
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(MostModule));
XmlTextReader reader = new XmlTextReader(FILENAME);
MostModule MostModule = (MostModule)xs.Deserialize(reader);
}
}
[XmlRoot("Function")]
public class Function
{
[XmlElement("double")]
public List<int> m_double {get;set;}
}
public class SSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlElement("Function")]
public Function Coefficient { get; set; }
}
[XmlRoot("SingleSensors")]
public class SimpleSensor
{
[XmlElement("SSensor")]
public SSensor sSensor { get; set; }
}
[XmlRoot("PressureSensors")]
public class PressureSensors
{
[XmlElement("PSensor")]
public SSensor sSensor { get; set; }
}
[XmlRoot("Module")]
public class MostModule
{
[XmlElement("SingleSensors")]
public SimpleSensor Sensor { get; set; }
[XmlElement("PressureSensors")]
public PressureSensors PressureSensor { get; set; }
}
}

Categories