Question

I'm trying to import several different children from an XML file (namely, this one), and I can't seem to get the procedure down. I used an XSD auto-gen site (freeformatter - as I'm unfamiliar with xsd.exe) and then passed the XSD through Xsd2code to create a (designer) class for the list, but I'm pretty much lost at this point.

XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myanimelist">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="myinfo">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:int" name="user_id"/>
              <xs:element type="xs:string" name="user_name"/>
              <xs:element type="xs:byte" name="user_reading"/>
              <xs:element type="xs:byte" name="user_completed"/>
              <xs:element type="xs:byte" name="user_onhold"/>
              <xs:element type="xs:byte" name="user_dropped"/>
              <xs:element type="xs:byte" name="user_plantoread"/>
              <xs:element type="xs:float" name="user_days_spent_watching"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="manga" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:short" name="series_mangadb_id"/>
              <xs:element type="xs:string" name="series_title"/>
              <xs:element type="xs:string" name="series_synonyms"/>
              <xs:element type="xs:byte" name="series_type"/>
              <xs:element type="xs:short" name="series_chapters"/>
              <xs:element type="xs:byte" name="series_volumes"/>
              <xs:element type="xs:byte" name="series_status"/>
              <xs:element type="xs:string" name="series_start"/>
              <xs:element type="xs:string" name="series_end"/>
              <xs:element type="xs:anyURI" name="series_image"/>
              <xs:element type="xs:int" name="my_id"/>
              <xs:element type="xs:short" name="my_read_chapters"/>
              <xs:element type="xs:byte" name="my_read_volumes"/>
              <xs:element type="xs:string" name="my_start_date"/>
              <xs:element type="xs:string" name="my_finish_date"/>
              <xs:element type="xs:byte" name="my_score"/>
              <xs:element type="xs:byte" name="my_status"/>
              <xs:element type="xs:string" name="my_rereadingg"/>
              <xs:element type="xs:byte" name="my_rereading_chap"/>
              <xs:element type="xs:int" name="my_last_updated"/>
              <xs:element type="xs:string" name="my_tags"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Resulting Class

Though I have poked around quite a bit, I can't seem to come across a method that will let me import anything, though I've really just been trying whatever to import the data.

In short, what do I need to do to create a List object from a local copy of the list mentioned earlier titled "manga.xml"?

Again, I've tried multiple other articles, but I'm at the point where I feel it's best just to ask somebody.

Thanks guys.

Was it helpful?

Solution

Found a method that works / finally got how to use a basic LINQ select query. Query:

var doc = XDocument.Load(path);
                    var animes = from anime in doc.Descendants("anime")
                                 select new
                                 {
                                     series_animedb_id = anime.Element("series_animedb_id").Value,
                                     series_title = anime.Element("series_title").Value,
                                     series_synonyms = anime.Element("series_synonyms").Value,
                                     series_type = anime.Element("series_type").Value,
                                     series_episodes = anime.Element("series_episodes").Value,
                                     series_status = anime.Element("series_status").Value,
                                     series_start = anime.Element("series_start").Value,
                                     series_end = anime.Element("series_end").Value,
                                     series_image = anime.Element("series_image").Value,
                                     my_id = anime.Element("my_id").Value,
                                     my_watched_episodes = anime.Element("my_watched_episodes").Value,
                                     my_start_date = anime.Element("my_start_date").Value,
                                     my_finish_date = anime.Element("my_finish_date").Value,
                                     my_score = anime.Element("my_score").Value,
                                     my_status = anime.Element("my_status").Value,
                                     my_rewatching = anime.Element("my_rewatching").Value,
                                     my_rewatching_ep = anime.Element("my_rewatching_ep").Value,
                                     my_last_updated = anime.Element("my_last_updated").Value,
                                     my_tags = anime.Element("my_tags").Value

                                 };

                    foreach (var anime in animes)
                    {
                        var newAnime = new AnimeI(anime.series_animedb_id, anime.series_title, anime.series_synonyms,
                            anime.series_type, anime.series_episodes, anime.series_status, anime.series_start,
                            anime.series_end, anime.series_image, anime.my_id, anime.my_watched_episodes,
                            anime.my_start_date, anime.my_finish_date, anime.my_score, anime.my_status,
                            anime.my_rewatching, anime.my_rewatching_ep, anime.my_last_updated, anime.my_tags);
                        animeList.Add(newAnime);
                    }

Class file:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace MalApp2
{
    public class AnimeI
    {

        public override string ToString()
        {
            return this.Series_Title.ToString();
        }

        public AnimeI (string series_animedb_id, string series_title, string series_synonyms, string series_type,
            string series_episodes, string series_status, string series_start, string series_end, string series_image,
            string my_id, string my_watched_episodes, string my_start_date, string my_finish_date, string my_score,
            string my_status, string my_rewatching, string my_rewatching_ep, string my_last_updated,
            string my_tags)
        {
            this.Series_Animedb_Id = series_animedb_id;
            this.Series_Title = series_title;
            this.Series_Synonyms = series_synonyms;
            this.Series_Type = series_type;
            this.Series_Episodes = series_episodes;
            this.Series_Status = series_status;
            this.Series_Start = series_start;
            this.Series_End = series_end;
            this.Series_Image = series_image;
            this.My_Id = my_id;
            this.My_Watched_Episodes = my_watched_episodes;
            this.My_Start_Date = my_start_date;
            this.My_Finish_Date = my_finish_date;
            this.My_Score = my_score;
            this.My_Status = my_status;
            this.My_Rewatching = my_rewatching;
            this.My_Rewatching_Ep = my_rewatching_ep;
            this.My_Last_Updated = my_last_updated;
            this.My_Tags = my_tags;
        }
        public AnimeI()
        {

        }

        public string Series_Animedb_Id { get; set; }

        public string Series_Title { get; set; }

        public string Series_Synonyms { get; set; }

        public string Series_Type { get; set; }

        public enum SeriesTypeEnum
        {
            Unknown = 0,
            Tv = 1,
            Ova = 2,
            Movie = 3,
            Special = 4,
            Ona = 5,
            Music = 6
        }

        public string Series_Episodes { get; set; }

        public string Series_Status { get; set; }

        public enum Series_StatusEnum
        {
            Watching = 1,
            Completed = 2,
            OnHold = 3,
            Dropped = 4,
            PlanToWatch = 6
        }

        public string Series_Start { get; set; }

        public string Series_End { get; set; }

        public string Series_Image { get; set; }

        public string My_Id { get; set; }

        public string My_Watched_Episodes { get; set; }

        public string My_Start_Date { get; set; }

        public string My_Finish_Date { get; set; }

        public string My_Score { get; set; }

        public string My_Status { get; set; }

        public string My_Rewatching { get; set; }

        public string My_Rewatching_Ep { get; set; }

        public string My_Last_Updated { get; set; }

        public string My_Tags { get; set; }
    }
}

(the enums are currently unused)

Hope this helps others with similar issues

Note: This is for the anime class, but the structure of the XML is fundamentally the same as it's manga counterpart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top