Reputation: 9037
I have the xml below and I am wondering how can it be deserialized to a set of classes without having to add tons of fields (to those classes) just for the sake of representing the different xml attributes (and their respective values).
I thought I could have something which support declarative xml attributes as .NET attributes on top properties that define xml elements.
<y:input xmlns:y='http://www.stuff.com/blahblah/42'>
<y:datas>
<y:instance yclass='Report' yid="report">
<language yid='LANG_fr'/>
<threshold>0.8</threshold>
<typePeriod>predefinedPeriod</typePeriod>
<interval>month</interval>
<valuePeriod>April</valuePeriod>
<fund yclass="Fund">
<name>K</name>
<performance yclass="Performance">
<typeValue>percent</typeValue>
<value>-0.05</value>
</performance>
[... lot of other fields ...]
</fund>
</y:instance>
</y:datas>
</y:input>
Upvotes: 0
Views: 342
Reputation: 34421
I like using xml linq along with a nested dictionary :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace yNs = doc.Root.GetNamespaceOfPrefix("y");
Datas data = doc.Descendants(yNs + "datas").Select(x => new Datas() {
instances = x.Descendants(yNs + "instance").Select(y => new Instance() {
instance = (string)y.Attribute("yid"),
language = (string)y.Element("language").Attribute("yid"),
threshold = (decimal)y.Element("threshold"),
typePeriod = (string)y.Element("typePeriod"),
interval = (string)y.Element("interval"),
valuePeriod = (string)y.Element("valuePeriod"),
fund = y.Elements("fund").Select(z => new Fund() {
fields = z.Elements().GroupBy(a => a.Name.LocalName, b => b.Elements()
.GroupBy(c => c.Name.LocalName, d => (string)d)
.ToDictionary(c => c.Key, d => d.FirstOrDefault()))
.ToDictionary(a => a.Key, b => b.FirstOrDefault())
}).FirstOrDefault()
}).ToList()
}).FirstOrDefault();
}
}
public class Datas
{
public List<Instance> instances { get; set; }
}
public class Instance
{
public string instance { get; set; }
public string language { get; set; }
public decimal threshold { get; set; }
public string typePeriod { get; set; }
public string interval { get; set; }
public string valuePeriod { get; set; }
public Fund fund { get; set; }
}
public class Fund
{
public Dictionary<string, Dictionary<string,string>> fields { get; set; }
}
}
Upvotes: 1
Reputation: 371
You should be able to use ExpandoObject (part of System.Dynamic).
I tried a quick solution myself and was able to parse that xml successfully to a dynamic object.
What you need to do is:
Parse the string data to XDocument so you have an xml document object.
var doc = XDocument.Parse(xmlData);
I then converted the doc to a json string, you don't need to do this but it was the quickest way for me to test if this will work. (For this I needed to add the Newtonsoft.Json NuGet package.)
string jsonText = JsonConvert.SerializeXNode(doc);
Then lastly I deserilzed the object like this:
dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
Upvotes: 1