I have looked at several other questions for this same issue but have not been able to resolve my problem. I have classes, code, and XML as follows. However, after the Deserialize call my type variable contains a TestList array which contains a TestElement but the TestElement is null. Appreciate any help. Thanks.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace TestXMLSerialization
{
class Program
{
static void Main(string[] args) {
XmlSerializer ser = new XmlSerializer(typeof(TestRootElement));
string xmlString = "<?xml version=\"1.0\" ?><TestRootElement><TestList><TestItem><TestElement>Test Data</TestElement></TestItem></TestList></TestRootElement>";
TestRootElement type = (TestRootElement)ser.Deserialize(new StringReader(xmlString));
Console.WriteLine(type.TestList[0].TestElement);
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("TestRootElement")]
public class TestRootElement
{
[System.Xml.Serialization.XmlElement("TestList")]
public List<TestItem> TestList { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlType("TestItem")]
public class TestItem
{
[System.Xml.Serialization.XmlElement("TestElement")]
public string TestElement { get; set; }
}
}
Turns out the code works fine without any Serialization attributes.
public class TestRootElement
{
public List<TestItem> TestList { get; set; }
}
public class TestItem
{
public string TestElement { get; set; }
}
So adding the attributes back in one at a time I found that the List<> needed an XmlArray attribute instead of XmlElement.
[Serializable()]
[System.Xml.Serialization.XmlRoot("TestRootElement")]
public class TestRootElement
{
[XmlArray("TestList")]
public List<TestItem> TestList { get; set; }
}
[Serializable()]
[XmlType("TestItem")]
public class TestItem
{
[XmlElement("TestElement")]
public string TestElement { get; set; }
}
Related
I'm using the XmlSerializer in System.Xml.Serialization.
I have a list (or two lists really) separated by xsi:type.
<?xml version="1.0" encoding="utf-8"?>
<ButikerOmbud xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Info>
<Meddelande>blah blah</Meddelande>
</Info>
<ButikOmbud xsi:type="StoreAssortmentViewModel">
<Typ>Butik</Typ><Nr>2515</Nr>
</ButikOmbud>
<ButikOmbud xsi:type="StoreAssortmentViewModel">
<Typ>Butik</Typ><Nr>2516</Nr>
</ButikOmbud>
<ButikOmbud xsi:type="AgentAssortmentViewModel">
<Typ>Ombud</Typ><Nr>011703-91A</Nr>
</ButikOmbud>
<ButikOmbud xsi:type="AgentAssortmentViewModel">
<Typ>Ombud</Typ><Nr>011703-92B</Nr>
</ButikOmbud>
</ButikerOmbud>
I've created some classes that map to this:
[XmlRoot(ElementName = "ButikerOmbud")]
public class ButiksCollection
{
[XmlElement(ElementName = "Info")]
public Info Info { get; set; }
[XmlElement(ElementName = "ButikOmbud")]
public List<Butik> Butiker { get; set; }
}
[XmlRoot(ElementName = "ButikOmbud")]
[XmlType(TypeName = "StoreAssortmentViewModel")]
public class Butik
{
[XmlElement(ElementName = "Typ")]
public string Typ { get; set; }
[XmlElement(ElementName = "Nr")]
public int Nr { get; set; }
}
and then I'll do this
(ButiksCollection)(new XmlSerializer(typeof(ButiksCollection)).Deserialize(memoryStream));
This should work if only the StoreAssortmentViewModel existed. But given that there exists AgentAssortmentViewModel under the same node. I'm not sure how I should de-serialise this. I'm assuming there should be another collection List<Butik> Agents on ButiksCollection.
The only attribute I've found that seems to map to xsi:type is applied to classes, which I don't think is what I want here
How do I arrange and attribute my classes so this will de-serialize?
Here's all that on dotnetfiddle: https://dotnetfiddle.net/vmT4SK
Try following :
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(ButiksCollection));
ButiksCollection butik = (ButiksCollection)serializer.Deserialize(reader);
List<StoreAssortmentViewModel> storeAssortments = butik.Butiker.Where(x => x.GetType() == typeof(StoreAssortmentViewModel)).Select(x => (StoreAssortmentViewModel)x).ToList();
List<AgentAssortmentViewModel> agentAssortments = butik.Butiker.Where(x => x.GetType() == typeof(AgentAssortmentViewModel)).Select(x => (AgentAssortmentViewModel)x).ToList();
}
}
[XmlRoot(ElementName = "ButikerOmbud")]
public class ButiksCollection
{
[XmlElement(ElementName = "Info")]
public Info Info { get; set; }
[XmlElement(ElementName = "ButikOmbud")]
public List<Butik> Butiker { get; set; }
}
[XmlRoot(ElementName = "ButikOmbud")]
[XmlInclude(typeof(StoreAssortmentViewModel))]
[XmlInclude(typeof(AgentAssortmentViewModel))]
public class Butik
{
[XmlElement(ElementName = "Typ")]
public string Typ { get; set; }
[XmlElement(ElementName = "Nr")]
public string Nr { get; set; }
}
public class Info
{
public string Meddelande { get; set; }
}
public class StoreAssortmentViewModel : Butik
{
}
public class AgentAssortmentViewModel : Butik
{
}
}
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);
}
}
}
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/ ! :)
I'm having difficulty parsing an XML string where the parent and child nodes have the same tag name. Obviously, I could replace the open/close tags with empty strings and parse with the code below, but that's not elegant.
I've searched and see that there are answers for how to do this with XDocument, but I specifically would like to do this with XmlSerializer (if possible).
Below is a minimal, reproducable example.
Example XML:
<AddJob>
<AddJob RequestStatus="OK" RequestMessage="Job successfuly added [testPrintServer.tif, PES_Carpet_16C_76.2 x 50.8 dpi_170517_Normal]" UUID="74ad5971-7baf-49ce-b85b-ee08188d5721" />
</AddJob>
Parsing code:
public class XmlHelper
{
public static T Deserialize<T>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
T result;
using (var reader = new StringReader(xml))
{
result = (T)serializer.Deserialize(reader);
}
return result;
}
}
Data model:
[XmlRoot("AddJob")]
public class AddJob
{
[XmlAttribute]
public string RequestStatus { get; set; }
[XmlAttribute]
public string RequestMessage { get; set; }
[XmlAttribute("UUID")]
public string RipJobId { get; set; }
}
Calling code:
var addedJobResponse = XmlHelper.Deserialize<AddJob>(exampleXml);
Your data model doesn't match your xml structure.
Please use something like that:
[XmlRoot("AddJob")]
public class AddJob
{
[XmlElement(ElementName = "AddJob")]
public List<NestedAddJob> AddJobs { get; set; }
}
public class NestedAddJob
{
[XmlAttribute]
public string RequestStatus { get; set; }
[XmlAttribute]
public string RequestMessage { get; set; }
[XmlAttribute("UUID")]
public string RipJobId { get; set; }
}
The nested AddJob elements look like an array and you cannot have an array at the root. So add a Root class like code below :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication75
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = "<AddJob>" +
"<AddJob RequestStatus=\"OK\" RequestMessage=\"Job successfuly added [testPrintServer.tif, PES_Carpet_16C_76.2 x 50.8 dpi_170517_Normal]\" UUID=\"74ad5971-7baf-49ce-b85b-ee08188d5721\" />" +
"</AddJob>";
Root job = XmlHelper.Deserialize<Root>(xml);
}
}
public class XmlHelper
{
public static T Deserialize<T>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
T result;
using (var reader = new StringReader(xml))
{
result = (T)serializer.Deserialize(reader);
}
return result;
}
}
[XmlRoot("AddJob")]
public class Root
{
public AddJob AddJob { get; set; }
}
public class AddJob
{
[XmlAttribute]
public string RequestStatus { get; set; }
[XmlAttribute]
public string RequestMessage { get; set; }
[XmlAttribute("UUID")]
public string RipJobId { get; set; }
}
}
I am trying to serialize an object but I am facing some issues regarding the attributes of a parent element that contains an array.
I have the following xml structure and I can't add the attribute in RatePlans element.
<Root>
<RatePlans Attribute="??this one??">
<RatePlan Attribute1="RPC" Attribute2="MC" Attribute3="RPT">
.
.
.
</RatePlan>
<RatePlan Attribute1="RPC2" Attribute2="MC3" Attribute3="RPT4">
.
.
.
</RatePlan>
</RatePlans>
</Root>
This is what I have done so far:
namespace XmlT {
[Serializable]
[XmlRoot("Root")]
public class Root {
public List<RatePlan> RatePlans { get; set; }
}
}
namespace XmlT {
[Serializable]
public class RatePlan {
[XmlAttribute]
public string RatePlanCode { get; set; }
[XmlAttribute]
public string MarketCode { get; set; }
[XmlAttribute]
public string RatePlanType { get; set; }
}
}
This gives me a correct structure but I don't know how to add the attribute I want
Another approach
I've tried also another approach but this gives me wrong values at all.
namespace XmlT {
[Serializable]
[XmlRoot("Root")]
public class Root {
public RatePlans RatePlans { get; set; }
}
}
namespace XmlT {
[Serializable]
public class RatePlans {
[XmlAttribute]
public string HotelCode { get; set; }
public List<RatePlan> RatePlan { get; set; }
}
}
EDIT
this the method that I am using for the serialization
protected static string Serialize<T>(object objToXml, bool IncludeNameSpace = false) where T : class {
StreamWriter stWriter = null;
XmlSerializer xmlSerializer;
string buffer;
try {
xmlSerializer = new XmlSerializer(typeof(T));
MemoryStream memStream = new MemoryStream();
stWriter = new StreamWriter(memStream);
if (!IncludeNameSpace) {
var xs = new XmlSerializerNamespaces();
xs.Add("", "");
xmlSerializer.Serialize(stWriter, objToXml, xs);
} else {
xmlSerializer.Serialize(stWriter, objToXml);
}
buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
} catch (Exception Ex) {
throw Ex;
} finally {
if (stWriter != null) stWriter.Close();
}
return buffer;
}
Does anyone know how could I do this?
Thanks
If the RatePlans class from the 2nd example inherits from List<RatePlan> you will get the desired result:
[Serializable]
public class RatePlans: List<RatePlan>
{
[XmlAttribute]
public string HotelCode { get; set; }
}
Edit:
My bad. Fields of classes inheriting from collections will not be serialized. I didn't knew that. Sorry...
However, this solution works:
[Serializable]
[XmlRoot("Root")]
public class Root
{
public RatePlans RatePlans { get; set; }
}
[Serializable]
public class RatePlans
{
[XmlAttribute]
public string HotelCode { get; set; }
[XmlElement("RatePlan")]
public List<RatePlan> Items = new List<RatePlan>();
}