Is it possible to deserialize XML into List<T>? - c#

Given the following XML:
<?xml version="1.0"?>
<user_list>
<user>
<id>1</id>
<name>Joe</name>
</user>
<user>
<id>2</id>
<name>John</name>
</user>
</user_list>
And the following class:
public class User {
[XmlElement("id")]
public Int32 Id { get; set; }
[XmlElement("name")]
public String Name { get; set; }
}
Is it possible to use XmlSerializer to deserialize the xml into a List<User> ? If so, what type of additional attributes will I need to use, or what additional parameters do I need to use to construct the XmlSerializer instance?
An array ( User[] ) would be acceptable, if a bit less preferable.

You can encapsulate the list trivially:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("user_list")]
public class UserList
{
public UserList() {Items = new List<User>();}
[XmlElement("user")]
public List<User> Items {get;set;}
}
public class User
{
[XmlElement("id")]
public Int32 Id { get; set; }
[XmlElement("name")]
public String Name { get; set; }
}
static class Program
{
static void Main()
{
XmlSerializer ser= new XmlSerializer(typeof(UserList));
UserList list = new UserList();
list.Items.Add(new User { Id = 1, Name = "abc"});
list.Items.Add(new User { Id = 2, Name = "def"});
list.Items.Add(new User { Id = 3, Name = "ghi"});
ser.Serialize(Console.Out, list);
}
}

If you decorate the User class with the XmlType to match the required capitalization:
[XmlType("user")]
public class User
{
...
}
Then the XmlRootAttribute on the XmlSerializer ctor can provide the desired root and allow direct reading into List<>:
// e.g. my test to create a file
using (var writer = new FileStream("users.xml", FileMode.Create))
{
XmlSerializer ser = new XmlSerializer(typeof(List<User>),
new XmlRootAttribute("user_list"));
List<User> list = new List<User>();
list.Add(new User { Id = 1, Name = "Joe" });
list.Add(new User { Id = 2, Name = "John" });
list.Add(new User { Id = 3, Name = "June" });
ser.Serialize(writer, list);
}
...
// read file
List<User> users;
using (var reader = new StreamReader("users.xml"))
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<User>),
new XmlRootAttribute("user_list"));
users = (List<User>)deserializer.Deserialize(reader);
}
Credit: based on answer from YK1.

Yes, it will serialize and deserialize a List<>. Just make sure you use the [XmlArray] attribute if in doubt.
[Serializable]
public class A
{
[XmlArray]
public List<string> strings;
}
This works with both Serialize() and Deserialize().

I think I have found a better way. You don't have to put attributes into your classes. I've made two methods for serialization and deserialization which take generic list as parameter.
Take a look (it works for me):
private void SerializeParams<T>(XDocument doc, List<T> paramList)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(paramList.GetType());
System.Xml.XmlWriter writer = doc.CreateWriter();
serializer.Serialize(writer, paramList);
writer.Close();
}
private List<T> DeserializeParams<T>(XDocument doc)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
System.Xml.XmlReader reader = doc.CreateReader();
List<T> result = (List<T>)serializer.Deserialize(reader);
reader.Close();
return result;
}
So you can serialize whatever list you want! You don't need to specify the list type every time.
List<AssemblyBO> list = new List<AssemblyBO>();
list.Add(new AssemblyBO());
list.Add(new AssemblyBO() { DisplayName = "Try", Identifier = "243242" });
XDocument doc = new XDocument();
SerializeParams<T>(doc, list);
List<AssemblyBO> newList = DeserializeParams<AssemblyBO>(doc);

Yes, it does deserialize to List<>. No need to keep it in an array and wrap/encapsulate it in a list.
public class UserHolder
{
private List<User> users = null;
public UserHolder()
{
}
[XmlElement("user")]
public List<User> Users
{
get { return users; }
set { users = value; }
}
}
Deserializing code,
XmlSerializer xs = new XmlSerializer(typeof(UserHolder));
UserHolder uh = (UserHolder)xs.Deserialize(new StringReader(str));

Not sure about List<T> but Arrays are certainly do-able. And a little bit of magic makes it really easy to get to a List again.
public class UserHolder {
[XmlElement("list")]
public User[] Users { get; set; }
[XmlIgnore]
public List<User> UserList { get { return new List<User>(Users); } }
}

How about
XmlSerializer xs = new XmlSerializer(typeof(user[]));
using (Stream ins = File.Open(#"c:\some.xml", FileMode.Open))
foreach (user o in (user[])xs.Deserialize(ins))
userList.Add(o);
Not particularly fancy but it should work.

Yes, you can deserialize into List<User> or User[] with the XmlSerializer. I would prefer List<User> over User[]. Note that XmlSerializer does not support deserialization into interfaces, so you cannot deserialize into ICollection<T>, IReadOnlyCollection<T> or IList<T> as this will fail with a NotSupportedException.

Related

Json Deserializing not populating properties

I'm deserializing a third party string like this:
{"status":4,"errors":[{"Duplicate Application":"Duplicate Application"}]}
I use my standard extension method:
public static T DeserializeJson<T>(string response)
where T : class
{
var s = new DataContractJsonSerializer(typeof(T));
try {
using (var ms = new MemoryStream()) {
byte[] data = System.Text.Encoding.UTF8.GetBytes(response);
ms.Write(data, 0, data.Length);
ms.Position = 0;
return (T)s.ReadObject(ms);
}
}
catch {
return default(T);
}
}
The class I'm trying to deserialize to looks like this:
[DataContract]
public class ResponseProps
{
[DataMember(Name = "status", Order = 0)]
public string ResponseCode { get; set; }
[DataMember(Name = "lead_id", Order=1)]
public string LeadId { get; set; }
[DataMember(Name = "price", Order=2)]
public decimal Price { get; set; }
[DataMember(Name = "redirect_url", Order = 3)]
public string RedirectUrl { get; set; }
[DataMember(Name = "errors", Order = 4)]
public List<Dictionary<string, string>> Errors { get; set; }
}
I'm using a List of type Dictionary (string, string) for the Errors property because other types I've tried have broken the deserializer - this works in that the serializer no longer throws an exception.
However, I'm now trying to retrieve the data from Errors - I'm using this code:
var cr = XmlHelper.DeserializeJson<ResponseProps>(response);
var errorStore = new HashSet<string>();
foreach (var dict in cr.Errors)
{
foreach (var kvp in dict)
{
errorStore.Add(kvp.Key + ": " + kvp.Value);
}
}
I've done various tests - the dict counts 1, but there are no kvp, so when the loop runs I get no messages.
I'm guessing this is again down to deserialization rather than incorrect looping, but I've not been able to fix it.
Anyone got any advice?
To be able to deserialize such dictionary, you should customize settings of DataContractJsonSerializer with UseSimpleDictionaryFormat set to true:
DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings
{
UseSimpleDictionaryFormat = true
};
var s = new DataContractJsonSerializer(typeof(T), settings);
By the way, do you have any reason to use DataContractJsonSerializer with your custom DeserializeJson<T>() method? You could do the same in one line of code with JSON.Net:
var obj = JsonConvert.DeserializeObject<ResponseProps>(str);
JSON.Net is also much more flexible and have a better performance than DataContractJsonSerializer.

Deserialising XML with different element name in c#

XML:
<?xml version="1.0" encoding="UTF-8"?>
<Images>
<I0>
<Path>123.com</Path>
<I0>
<I1>
<Path>123.com</Path>
<I1>
<I2>
<Path>123.com</Path>
<I2>
</Images>
Can serializer.Deserialize() be used to get tags with different names into a collection?
currently, in my object I have:
C#:
public class rootObject
{
[XmlElement(ElementName = "I0")]
public I0 I0 { get; set; }
[XmlElement(ElementName = "I1")]
public I1 I1 { get; set; }
[XmlElement(ElementName = "I2")]
public I2 I2 { get; set; }
}
But I would like to have (Because Images can have more or fewer elements):
public class rootObject
{
public List<I> Is { get; set; }
}
You can do what you are suggesting you just merely need to pass in the type argument in your class doing the generic. The key point to remember when you do a deserialization routine is that the routine needs to know the sub reference. So if I was to say string.Deserialize it would bomb. It would need to know a reference string.Deserialize> where Sub could be the class object that may change.
Say I have a base class and I want 'T' to be a type I can change for extensible abilities later.
[Serializable]
public class Test<T> where T : class
{
public Test() { }
public int TestId { get; set; }
public string Name { get; set; }
public List<T> Shipments { get; set; }
}
I want to test this with two classes I just make up that have different properties slightly
[Serializable]
public class Sub1
{
public int Id { get; set; }
public string Desc { get; set; }
}
[Serializable]
public class Sub2
{
public int IdWhatever { get; set; }
public string DescWhatever { get; set; }
}
Now let's do a main program and test serialization.
class Program
{
static void Main(string[] args)
{
var serializeTest = new Test<Sub1> { TestId = 1, Name = "Test", Shipments = new List<Sub1> { new Sub1 { Id = 1, Desc = "Test" }, new Sub1 { Id = 2, Desc = "Test2" } } };
var serializeTest2 = new Test<Sub2> { TestId = 1, Name = "Test", Shipments = new List<Sub2> { new Sub2 { IdWhatever = 1, DescWhatever = "Test" }, new Sub2 { IdWhatever = 2, DescWhatever = "Test2" } } };
var serialized = serializeTest.SerializeToXml();
var serialized2 = serializeTest2.SerializeToXml();
var deserialized = serialized.DeserializeXml<Test<Sub1>>();
var deserialized2 = serialized2.DeserializeXml<Test<Sub2>>();
Console.WriteLine(serialized);
Console.WriteLine();
Console.WriteLine(serialized2);
Console.ReadLine();
}
}
And my Serialize and DeSerialize extension methods:
public static string SerializeToXml<T>(this T valueToSerialize, string namespaceUsed = null)
{
var ns = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, (namespaceUsed != null) ? namespaceUsed : string.Empty) });
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
dynamic xmler = new XmlSerializer(typeof(T));
xmler.Serialize(writer, valueToSerialize, ns);
}
return sw.ToString();
}
}
public static T DeserializeXml<T>(this string xmlToDeserialize)
{
dynamic serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlToDeserialize))
{
return (T)serializer.Deserialize(reader);
}
}
You don't need to specify the XmlElement name when the properties match the XML. A few solutions, some kinda hacky :).
HACKY: use regex string replace to replace <I#> and </I#> to
just <I> and </I>
SOMEWHAT HACKY: This might work for you:
How to deserialize an XML array containing multiple types of elements in C#,
but you'd have to add an attribute for i0, i1 ... i100, etc.
BEST: Is that your entire XML? I'd honestly just use LINQToXml and
do a Descendants("Path") and get an array of strings back with 1 line of code. Serialization is not really the best solution for this.

Deserializing xml with inner array

I'm trying to deserialize simple xml file:
<thesaurus xmlns="http://marklogic.com/xdmp/thesaurus">
<metadata>
</metadata>
<entry>
<term>a</term>
<synonym>
<term>as</term>
</synonym>
</entry>
<entry>
<term>b</term>
<synonym>
<term>bs</term>
</synonym>
<synonym>
<term>bss</term>
</synonym>
</entry>
</thesaurus>
I'm using XmlSerializer like this:
var xmlSerializer = new XmlSerializer(typeof(Thesaurus));
var thesaurus = xmlSerializer.Deserialize(stream);
My model looks like this:
[Serializable]
[XmlRoot("thesaurus", Namespace = "http://marklogic.com/xdmp/thesaurus")]
public class Thesaurus
{
[XmlElement("metadata")]
public Metadata Metadata { get; set; }
[XmlElement("entry")]
public List<Entry> Entries { get; set; }
}
public class Metadata
{
}
public class Entry
{
[XmlElement("term")]
public string Term { get; set; }
[XmlElement("synonym")]
public String[] Synonym { get; set; }
}
So when I'm running this code, I get deserialized thesaurus object with parsed metadata and 1 entry with filled term and synonym fields. I can't get all of the entries here.
BUT
when I comment out Synonym field it starts giving me 2 entries in thesaurus object. I can't wrap entries in <entries> tag because it's some internal format of an application I'm feeding with this xml file.
Anyone has any ideas how to parse this xml file correctly? I tried searching for a solution, but this xml looks quite different than ones in examples.
Ok, so if you need to keep inside synonim field array of terms fields you need to change your Entry class to something like this:
public class Entry
{
[XmlElement("term")]
public string Term { get; set; }
[XmlElement("synonim")]
public Term[] Synonym { get; set; }
}
also you'll need to add additional one:
public class Term
{
[XmlElement("term")]
public string Value { get; set; }
}
This way you'll have what you need.
So, additional hierarchy level was added by additional class.
Please find below code for your test:
var xmlSerializer = new XmlSerializer(typeof(Thesaurus));
var r = new Thesaurus();
r.Entries = new List<Entry>();
r.Metadata = new Metadata();
r.Entries.Add(new Entry()
{
Synonym = new Term[] { new Term(){Value = "1"}, new Term() {Value = "2"}, },
Term = "Term1"
});
r.Entries.Add(new Entry()
{
Synonym = new Term[] { new Term() { Value = "3" }, new Term() { Value = "4" }, },
Term = "Term2"
});
using (TextWriter writer = new StreamWriter(#"c:\111.xml"))
{
xmlSerializer.Serialize(writer, r);
writer.Close();
}
using (TextReader reader = new StreamReader(#"c:\111.xml"))
{
Thesaurus tt = xmlSerializer.Deserialize(reader) as Thesaurus;
Console.Write(tt.Entries.Count);
reader.Close();
}

Binding XML data to Model in C# MVC

I'm looking to bind XML to Model in C# MVC app.
XML:
<people>
<person>
<name>Mr Brown</name>
<age>40</age>
<hobby>
<title>Eating</title>
<description>eats a lot</description>
</hobby>
<hobby>
<title>Sleeping</title>
<description>sleeps a lot</description>
</hobby>
</person>
<person>
<name>Mr White</name>
<age>40</age>
<hobby>
<title>Skiing</title>
<description>more details on this</description>
</hobby>
<hobby>
<title>Football</title>
<description>watches football</description>
</hobby>
</person>
</people>
Model:
public class People
{
public string Name { get; set; }
public string Age { get; set; }
public IList<Hobbies> Hobby {get; set; }
}
public class Hobbies
{
public string Title { get; set; }
public string Description { get; set; }
}
Broken Binding:
var person = from a in xml.Descendants("person")
select new People
{
Name = a.Element("name").Value,
Age = a.Element("age").Value,
Hobby = *WHAT GOES HERE?*
}
I'n new to C# and looking for the best way to bind the data from the XML to the person var. Which I'll later loop over and output in an HTML table.
Any help would be great.
You have to do it this way:
var person = from a in xml.Descendants("person")
select new People
{
Name = a.Element("name").Value,
Age = a.Element("age").Value,
Hobby = a.Descendants("hobby")
.Select(x=> new Hobbies
{
Title =x.Element("title").Value,
Description = x.Element("description").Value
}).ToList()
};
WORKING FIDDLE:
https://dotnetfiddle.net/2uKdd5
Looks like you want standard XML deserialization. Some good answers on the best way to do that here
I would use XmlSerializer to load from Xml and to save to xml.
You can derive People from this class for example (SerializeManagement) :
public class SerializeManagement<T>
{
public static T ReadFromXML(string iPath)
{
T val = default(T);
try
{
// load from XML
using (var sw = new StreamReader(iPath, Encoding.Default))
{
var ser = new XmlSerializer(typeof(T));
val = (T)ser.Deserialize(sw);
}
}
catch
{
Console.WriteLine("Problem reading from xml data file.");
}
return val;
}
public void SaveToXML(string iPath)
{
try
{
//TODO => using
var sw = new StreamWriter(iPath, false, Encoding.Default);
var ser = new XmlSerializer(typeof(T));
ser.Serialize(sw, this);
sw.Close();
}
catch
{
Console.WriteLine("Problem saving to xml data file.");
}
}
}
If you encounter problems, this could be because of your model definition or xml structure :
Then you can :
1) Generate c# class from the xml using xsd utility;
2) Generate XML from existing class using SaveToXML. That way you are sure the XML structure is compliant with your model.
Enjoy !

Serialization Fail on list of lists using DataContractSerializer

I have some class that has a property of type List<object> I need to serialize that class to XML file using DataContractSerializer.
The serialization fails on ArgumentException when the object is a List<T>/IEnumerator<T>exception message:
Invalid name character in
'System.Collections.Generic.List`1[[MyProj.Result, MyProj,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
Here is the code sample that fails
The Class that takes the List<object>
[DataContract(IsReference = true)]
public class RecoveryMethodData
{
[DataMember]
public List<object> Parameters { get; set; }
public RecoveryMethodData()
{
Parameters = new List<object>();
}
public static void SerializeToFile(RecoveryMethodData recoveryMethodData, string fileName)
{
var encoding = Encoding.UTF8;
using (var fileWriter = new XmlTextWriter(fileName, encoding))
{
fileWriter.Formatting = Formatting.Indented;
// use SharedTypeResolver for deserializing assistance.
var serializer = new DataContractSerializer(typeof(RecoveryMethodData), null, int.MaxValue, false, true, null, new SharedTypeResolver());
serializer.WriteObject(fileWriter, recoveryMethodData);
}
}
}
Here is the usage:
private void TestSerialization()
{
var methodData = new RecoveryMethodData();
var result = new Result() {Message = "wow", Pass = true, FileName = "somefile "};
methodData.Parameters.Add(result);
methodData.Parameters.Add(true);
var list1 = new List<Result>();
list1.Add(new Result(){FileName = "in list1", Message = "in l 1"});
list1.Add(new Result(){FileName = "in list2", Message = "in l 2"});
methodData.Parameters.Add(list1);
RecoveryMethodData.SerializeToFile(methodData,#"C:\serialization_result.xml");
}
public class Result
{
public string Message { get; set; }
public string FileName { get; set; }
}
If I do not add list1 into the methodData.Parameters there is no problem serializing the methodDatad object.
One big limitation is that I can't know in advance which kind of objects will be added to the Parameters property (that is why it is a list of objects)
In order to DataContractSerializer to serialize an object, it shall know the types of all datamembers. In your case, you do not define a specific type but an object type. Try changing the definition of
public List<object> Parameters { get; set; }
to something like:
public List<IMyObject> Parameters { get; set; }
Note that, all of your objects which you are trying to add to the parameters list shall inherit IMyObject interface.
Update: I refactored your code up to some point (still in a bad shape) and it seems working, please have a try;
public class Tester
{
public Tester()
{
this.TestSerialization();
}
public void SerializeToFile(RecoveryMethodData recoveryMetaData,string fileName)
{
var encoding = Encoding.UTF8;
using (var fileWriter = new XmlTextWriter(fileName, encoding))
{
fileWriter.Formatting = Formatting.Indented;
// use SharedTypeResolver for deserializing assistance.
var serializer = new DataContractSerializer(typeof(RecoveryMethodData),new List<Type>(){typeof(bool),typeof(Result),typeof(List<Result>)});
serializer.WriteObject(fileWriter,recoveryMetaData);
}
}
private void TestSerialization()
{
var methodData = new RecoveryMethodData();
var result = new Result() { Message = "wow", Pass = true, FileName = "somefile " };
methodData.Add(result);
methodData.Add(true);
var list1 = new List<Result>();
list1.Add(new Result() { FileName = "in list1", Message = "in l 1" });
list1.Add(new Result() { FileName = "in list2", Message = "in l 2" });
methodData.Add(list1);
SerializeToFile(methodData, #"C:\serialization_result.xml");
}
}
public class Result
{
public string Message { get; set; }
public string FileName { get; set; }
public bool Pass { get; set; }
}
public class RecoveryMethodData : List<object>
{
}

Categories

Resources