0

I've almost written a "webstore" asp.net mvc 3 app and I want to populate my database with some items from one web store. They provide information about items for their partners in an xml files. I've downloaded one but it's of 150 MB, so I can't just open it and look through the content.

The information in the file also will not fully match my model, so I somehow need to "merge" things. How could I do it? Maybe you know some great tutorial on how to download xml and deserialize it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Aleksei Chepovoi
  • 3,915
  • 8
  • 39
  • 77
  • You've listed at least 3 different thing in your question (how to download XML, how to handle "large" XML and how to read data of "unknown" structure into your data storage). It is not clear what you have problem with - please be more specific... And make sure to search for answers first - I doubt you are the first person to download a file using C#/ASP.Net. – Alexei Levenkov Jan 25 '13 at 17:36
  • @AlexeiLevenkov, I've already downloaded xml file) so only two other problems remain. – Aleksei Chepovoi Jan 25 '13 at 17:39

2 Answers2

2

LINQ to XML is an amazing provider. It will allow you to query your XML document in the form of XElements.

using System.Xml.Linq;

If you can download the xml in code via whatever uri they provide, you can load it into memory via System.IO.File and manipulate it with LINQ to XML.

string xml = new WebClient().DownloadString(url);

XDocument doc = XDocument.Parse(xml);

//item being the name of the node
var itemDescendent = doc.Descendants("item"); 

var query = from item in itemDescendent
            select new
            {
               itemId = item.Attribute("id"),
               itemName = item.Attribute("Name")
            };

foreach (item in query)
{
   //dostuff
}

This would select attributes from nodes, you'd need a slightly different method to get nodes within nodes.

colin-higgins
  • 1,087
  • 10
  • 14
0

use this or this

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

namespace xmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var articles = new Articles();
            articles.ArticleArray = new ArticlesArticle[2]
            {
                new ArticlesArticle()
                    {
                        Guid = Guid.NewGuid(),
                        Order = 1,
                        Type = "deal_abstract",
                        Title = "Abu Dhabi...",
                        Summary = "Abu Dhabi...",
                        ArticleDate = new DateTime(2011,2,24)
                    },
                new ArticlesArticle()
                    {
                        Guid = Guid.NewGuid(),
                        Order = 2,
                        Type = "deal_abstract",
                        Title = "Abu Dhabi...",
                        Summary = "China...",
                        ArticleDate = new DateTime(2011,2,23)
                    },
            };

            var sw = new StringWriter();
            var xmlSer = new XmlSerializer(typeof (Articles));
            var noNamespaces = new XmlSerializerNamespaces();
            noNamespaces.Add("", ""); 
            xmlSer.Serialize(sw, articles,noNamespaces);
            Console.WriteLine(sw.ToString());
        }
    }

    [XmlRoot(ElementName = "articles", Namespace = "", IsNullable = false)]
    public class Articles
    {
        [XmlElement("article")]
        public ArticlesArticle[] ArticleArray { get; set; }
    }

    public class ArticlesArticle
    {
        [XmlElement("guid")]
        public Guid Guid { get; set; }
        [XmlElement("order")]
        public int Order { get; set; }
        [XmlElement("type")]
        public string Type { get; set; }
        [XmlElement("textType")]
        public string TextType { get; set; }
        [XmlElement("id")]
        public int Id { get; set; }
        [XmlElement("title")]
        public string Title { get; set; }
        [XmlElement("summary")]
        public string Summary { get; set; }
        [XmlElement("readmore")]
        public string Readmore { get; set; }
        [XmlElement("fileName")]
        public string FileName { get; set; }
        [XmlElement("articleDate")]
        public DateTime ArticleDate { get; set; }
        [XmlElement("articleDateType")]
        public string ArticleDateType { get; set; }
    }
}
Community
  • 1
  • 1