-1

I want to create list of whitelist objects from the below XML.

Created a class WhiteList

class WhiteList
{
    public string ID { get; set; }
    public string From { get; set; }
    public string To { get; set; }
}

I need to create list of objects out of the XML

<Response>
  <list>
    <whitelist>
      <id>1234</id>
      <from>sdfkmsfk@wfgtitleco.com</from>
      <to>dsfgsdf@loansdfglosers.com</to>
    </whitelist>
    <whitelist>
      <id>1452</id>
      <from>mdsfgaursdfgdfmimi@gmail.com</from>
      <to>dfsgdscew@loansdfglosers.com</to>
    </whitelist>
    <whitelist>
      <id>9483</id>
      <from>prvs=17958ac559=dmcclain@wfgtitleco.com</from>
      <to>status@loansdfglosers.com</to>
    </whitelist>
    <whitelist>
      <id>5654</id>
      <from>ewrgre@loansdfglosers</from>
      <to>werferw@loansdfglosers.com</to>
    </whitelist>
  </list>
</Response>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

Here is a straightforward example using your XML document:

public static List<WhiteList> CreateObjects(XDocument xmldoc)
{
    var query = from data in xmldoc.Descendants("whitelist")
                select new WhiteList
                {
                    ID = (string)data.Element("id"),
                    From = (string)data.Element("from"),
                    To = (string)data.Element("to"),
                };

    var list = query.ToList();
    List<WhiteList> result = new List<WhiteList>();
    // now create the desierd list
    foreach (var x in list)
    {
        WhiteList ws = new WhiteList();
        ws.ID = x.ID;
        ws.From = x.From;
        ws.To = x.To;
        result.Add(ws);
    }

    return result;
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
1

A generic class that I have created for serializing and de-serializing XML to .net object and vice versa.

You need to add a helper class as below. Just change the namespace as your project.

 using System.IO;
using System.Xml.Serialization;

namespace YourProject
{
public class XMLParser<T> where T : class
{

    public static T Deserialize(string path)
    {
        T item;
        try
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                item = (T)serializer.Deserialize(reader);

                reader.Dispose();
            }
        }
        catch (System.Exception)
        {

            throw;
        }
        return item;
    }


    public static void Serialize(object item, string path)
    {
        var serializer = new XmlSerializer(typeof(T));
        using (var fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            using (var writer = new StreamWriter(fs))
            {
                serializer.Serialize(writer, item);
                writer.Close();
                writer.Dispose();
            }
        }
    }
}
}

Now your class is not complaint with XML so you need to change your class a bit.

class Response{
 public List<WhiteList> list{get; set;}
}
class WhiteList{
public string ID {get;set;}
public string From {get;set;}
public string To {get;set;}
}

Now you can call XMLParser class to serialize or deserialize XML or .net object. Below is the call

Just provide the path to XML

Response response = XMLParser<Response>.Deserialize([path of XML]);
subham
  • 164
  • 1
  • 14