-1

Below is the XML:

<columninfo name="f1config">
    <column name="col1,col2" fieldname="c1" type="string" />
    <column name="col3" fieldname="c3" type="date" format="ddMMMyyy"/>
    <column name="col4,col5" fieldname="c4" type="string" format="ddMMMyyy"/>
 </columninfo>

How to deserialize above into below class columninfo:

public class columninfo
{
 public string name {get;set;}
 public Dictionary<string,column> fieldList {get;set;}
}

public class column
{
  public string name {get;set;}
  public string fieldname {get;set}
  public string format {get;set;}
}

problem is for collection object - fieldList.

svick
  • 236,525
  • 50
  • 385
  • 514
dsi
  • 3,199
  • 12
  • 59
  • 102
  • with your current XML you cannot do this. You can verify it by yourself by going to **Visual Studio >> Edit >> Paste Special >> Paste XML as Classes**. There you'll see that the class hierarchy which you is defined is not like as generated by visual studio – J.SMTBCJ15 Mar 20 '17 at 09:35
  • I've just done that, it shows XML - column array. But need `dictionary`. – dsi Mar 20 '17 at 09:39
  • There are plenty of questions of how to serialize/deserialize dictionary if that is the question (e.g. [here](http://stackoverflow.com/q/12554186/1997232)). As for your question you can always [manually parse](http://stackoverflow.com/q/55828/1997232) xml. – Sinatr Mar 20 '17 at 09:39
  • Is it poss with both - dictionary and name property - `public string name {get;set;} public Dictionary fieldList {get;set;}` as how above XML? – dsi Mar 20 '17 at 09:42
  • @dsi you'll have to look at Sinatr comment then .. Otherwise there is no solution by doing it with standard XML parsing. You'll have to do it manually. – J.SMTBCJ15 Mar 20 '17 at 09:44

2 Answers2

1

Try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication48
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            columninfo.info = doc.Descendants("columninfo").Select(x => new columninfo() {
                name = (string)x.Attribute("name"),
                fieldList = columninfo.GetDictionary(x)
            }).ToList();

        }
    }
    public class columninfo
    {
        public static List<columninfo> info = new List<columninfo>();
        public string name {get;set;}
        public Dictionary<string,column> fieldList {get;set;}

        public static Dictionary<string,column> GetDictionary(XElement columninfo)
        {
            Dictionary<string,column> dict = new Dictionary<string,column>();

            foreach(XElement column in columninfo.Elements("column"))
            {
                string fieldname = (string)column.Attribute("fieldname");
                string _type = (string)column.Attribute("type");
                string name = (string)column.Attribute("name");

                column newCol;
                string[] names;
                switch(fieldname)
                {
                    case "c1" :
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c1", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c2", newCol);
                        break;

                    case "c3":
                        newCol = new column() { name = fieldname, format = _type, fieldname = fieldname };
                        dict.Add("c3", newCol);
                        break;

                    case "c4":
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c4", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c5", newCol);
                        break;
                }
            }

            return dict;
        }
    }

    public class column
    {
        public string name { get; set; }
        public string fieldname { get; set; }
        public string format { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

I have taken in two stage, 1) deserialize to object containing array and 2) then by linq converting to dictionary.

Thanks

dsi
  • 3,199
  • 12
  • 59
  • 102