簡體   English   中英

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

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

我幾乎寫了一個“webstore”asp.net mvc 3應用程序,我想用一個網上商店的一些項目填充我的數據庫。 它們在xml文件中提供有關其合作伙伴的項目的信息。 我已經下載了一個但它的150 MB,所以我不能打開它並查看內容。

文件中的信息也不能完全匹配我的模型,所以我不知何故需要“合並”東西。 我怎么能這樣做? 也許你知道如何下載xml和反序列化的一些很棒的教程?

LINQ to XML是一個了不起的提供商。 它允許您以XElements的形式查詢XML文檔。

using System.Xml.Linq;

如果您可以通過它們提供的任何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
}

這將從節點中選擇屬性,您需要稍微不同的方法來獲取節點內的節點。

使用這個這個

 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