【问题标题】:How to deseralize an XML thats a DataTable?如何反序列化作为 DataTable 的 XML?
【发布时间】:2017-06-04 06:02:58
【问题描述】:

我有一个 XML 文件,当我使用 XSD 工具从 XML 创建一个类对象时,我收到一条消息说“名为 'link' 的列已经属于此 DataTable:无法将嵌套表设置为相同名字。

我想知道如何正确反序列化此 XML 文件,以便在应用程序上显示数据。这个 XML 文件基本上就是 Rss 新闻提要数据。

您可以在此处查看整个 XML 文件结构:https://github.com/karimo94/XMLDemo/blob/master/leaguenews.xml

【问题讨论】:

标签: c# xml deserialization xml-deserialization


【解决方案1】:

看看这个设计

https://blogs.windows.com/buildingapps/2017/05/01/master-master-detail-pattern/#.WSrAkFuGID4.twitter#7gkbxLDhEVrcmL6M.97

DataSet ds = new DataSet();
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
        FileStream readStream = new FileStream("leaguenews.xml", FileMode.Open);
        ds = (DataSet)xmlSerializer.Deserialize(readStream);
        readStream.Close();
        dataGridView1.DataSource = ds.Tables[0];

【讨论】:

    【解决方案2】:

    尝试关注:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Data;
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test1.xml";
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("Title", typeof(string));
                dt.Columns.Add("Description", typeof(string));
                dt.Columns.Add("Link", typeof(string));
                dt.Columns.Add("IsPermaLink", typeof(Boolean));
                dt.Columns.Add("GUID", typeof(string));
                dt.Columns.Add("Publish Date", typeof(DateTime));
                dt.Columns.Add("Width", typeof(int));
                dt.Columns.Add("Height", typeof(int));
                dt.Columns.Add("URL", typeof(string));
    
                XDocument doc = XDocument.Load(FILENAME); //or uri
                List<XElement> items = doc.Descendants("item").ToList();
    
                foreach (XElement item in items)
                {
                    dt.Rows.Add(new object[] {
                        (string)item.Element("title"),
                        (string)item.Element("description"),
                        (string)item.Element("link"),
                        (Boolean)item.Element("guid").Attribute("isPermaLink"),
                        (string)item.Element("guid"),
                        (DateTime)item.Element("pubDate"),
                        (int)item.Elements().Where(x => x.Name.LocalName == "thumbnail").FirstOrDefault().Attribute("width"),
                        (int)item.Elements().Where(x => x.Name.LocalName == "thumbnail").FirstOrDefault().Attribute("height"),
                        (string)item.Elements().Where(x => x.Name.LocalName == "thumbnail").FirstOrDefault().Attribute("url")
                    });
                }
    
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我尝试了两种方法,老实说,我无法让它正确反序列化而没有错误。幸运的是,我能够使用 rss2json.com 并将 Rss 提要转换为我可以使用 Newtonsoft Json.Net 解析的 json

      【讨论】:

        猜你喜欢
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 2018-10-11
        • 1970-01-01
        • 2014-01-20
        相关资源
        最近更新 更多