0

I have an XML file, and when I used XSD tool to create a class object from the XML, I get a message saying "A column named 'link' already belongs to this DataTable: cannot set a nested table to the same name.

I want to know how I can correctly deserialize this XML file so that I can display the data on an app. This XML file is basically just Rss news feed data.

You can see the entire XML file structure here: https://github.com/karimo94/XMLDemo/blob/master/leaguenews.xml

Karim O.
  • 1,325
  • 6
  • 22
  • 36
  • We need to see a [mcve] that includes sample XML to be sure what is going on, but you might try generating POCOs using the xsd.exe option [`/classes`](https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx) rather than a `DataSet` using `/dataset`. – dbc Jun 04 '17 at 06:10
  • Related? [The Code return error A column named 'link' already belongs](https://stackoverflow.com/q/18209393/3744182). – dbc Jun 04 '17 at 06:14
  • To work with RSS you can use [SyndicationFeed](https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed(v=vs.110).aspx) class. – Alexander Petrov Jun 04 '17 at 08:10
  • @AlexanderPetrov SyndicationFeed class doesn't work for Xamarin (Mono) – Karim O. Jun 04 '17 at 08:29

3 Answers3

0

Look at this design

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];
0

Try followng :

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

        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

I tried both ways and honestly, I couldn't get it to deserialize properly without errors. Luckily, I was able to use rss2json.com and that converted the Rss feed into a json which I could parse using Newtonsoft Json.Net

Karim O.
  • 1,325
  • 6
  • 22
  • 36