Convert XML to classes (deserialize it) and save it into database

835 Views Asked by At

Hi i have more than 20 links of xml files. I searched a lot to convert my xml to c# and then deserialize it according to classes and then put it into the database but not found a good solution for that. Please help me how to do it.

http://kithnyc.com/sitemap_products_1.xml?from=60594372&to=9586327751

This is the link of database and some xml is below

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://kithnyc.com/</loc>
<changefreq>daily</changefreq>
</url>
<url>
<loc>
http://kithnyc.com/products/adidas-originals-nmd-city-sock-black-blue
</loc>
<lastmod>2016-09-12T11:04:04-04:00</lastmod>
<changefreq>daily</changefreq>
<image:image>
<image:loc>...</image:loc>
<image:title>adidas Originals NMD City Sock - Black / Blue</image:title>
</image:image>
</url>
<url>
<loc>
http://kithnyc.com/products/kith-logo-mousepad-white
</loc>
<lastmod>2016-12-23T00:01:41-05:00</lastmod>
<changefreq>daily</changefreq>
<image:image>
<image:loc>
https://cdn.shopify.com/s/files/1/0094/2252/products/20150810-_MG_2963.jpg?v=1482353363
</image:loc>
<image:title>Kith Logo Mousepad - White</image:title>
</image:image>
</url>
</urlset>

This is the xml i want to put all its nodes in the database like "Loc" , "lastmod" and "changefreq" etc. Please help me how to convert it into classes and then how to deserialize it. Thanks

2

There are 2 best solutions below

4
On

You can add following data classes and repository class in your code.You can just call Repository class with filepath and call its FindAll method.It returns Url list.

 public class Url
{
    public string Loc { get; set; }
    public string Changefreq { get; set; }
    public string Lastmod { get; set; }
    public Image Image { get; set; }

}
public class Image
{
    public string Loc { get; set; }
    public string Title { get; set; }
}
    public class Repository
{
    private XDocument xmlDatas;
    public Repository(string filePath)
    {
        xmlDatas = XDocument.Load(filePath);
    }
    public List<Url> FindAll()
    {
        return xmlDatas.Elements().Elements()
            .Select(url =>
            {
                Url data = new Url();
                foreach (var item in url.Elements())
                {
                    switch (item.Name.LocalName)
                    {
                        case "loc":
                            data.Loc = item.Value;
                            break;
                        case "changefreq":
                            data.Changefreq = item.Value;
                            break;
                        case "lastmod":
                            data.Lastmod = item.Value;
                            break;
                        case "image":
                            Image image = new Image();
                            foreach (var img in item.Elements())
                            {
                                switch (img.Name.LocalName)
                                {
                                    case "loc":
                                        image.Loc = img.Value;
                                        break;
                                    case "title":
                                        image.Title = image.Title;
                                        break;
                                }
                            }
                            data.Image = image;
                            break;
                    }
                    var s = item.Name.LocalName;
                }
                return data;
            }).ToList();
    }
}
4
On

Hi you can do it like this:

Step 1: Read the file by the below line

XDocument xdoc = XDocument.Load(filebyte);

Step 2: The get all node of xml by Descendants method and convert it into list. if need apply where clause to filter that data.

var pNodelist = xdoc.Root.Descendants().ToList().Where(i => i.Name.LocalName == "p");

Step 3: Create data table and store all the data in data table from list like

DataTable dt = new DataTable();
dt.Columns.Add("StartTime");
dt.Columns.Add("EndTime");
dt.Columns.Add("Message");
foreach (var data in pNodelist)
{
if (data.HasAttributes == true)
dt.Rows.Add(data.FirstAttribute.Value, data.LastAttribute.Value, data.Value);
}

Step 4 : Then you can save it db and you can avoid the step 4 and directly store date from list to data base