简体   繁体   English

如何在c#,asp.net mvc 3中反序列化未知的大型xml文件?

[英]How to deserialize unknown large xml file in c#, asp.net mvc 3?

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. 我几乎写了一个“webstore”asp.net mvc 3应用程序,我想用一个网上商店的一些项目填充我的数据库。 They provide information about items for their partners in an xml files. 它们在xml文件中提供有关其合作伙伴的项目的信息。 I've downloaded one but it's of 150 MB, so I can't just open it and look through the content. 我已经下载了一个但它的150 MB,所以我不能打开它并查看内容。

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? 也许你知道如何下载xml和反序列化的一些很棒的教程?

LINQ to XML is an amazing provider. LINQ to XML是一个了不起的提供商。 It will allow you to query your XML document in the form of XElements. 它允许您以XElements的形式查询XML文档。

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. 如果您可以通过它们提供的任何uri在代码中下载xml,您可以通过System.IO.File将其加载到内存中,并使用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. 这将从节点中选择属性,您需要稍微不同的方法来获取节点内的节点。

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; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM