需要帮助将XML数据导入C#对象

埃文斯

我正在尝试从XML文件(即这个文件)中导入几个不同的子级,但似乎无法简化该过程。我使用了XSD自动生成网站(freeformatter,因为我不熟悉xsd.exe),然后通过Xsd2code传递了XSD,从而为列表创建了一个(设计器)类,但是到那时我已经很迷茫了。

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>

结果类

尽管我已经摸索了很多,但我似乎并没有遇到可以让我导入任何内容的方法,尽管我实际上一直在尝试导入数据的任何方法。

简而言之,我需要做什么来从前面提到的名为“ manga.xml”的列表的本地副本创建List对象?

同样,我尝试了其他多篇文章,但是我觉得最好是问一个人。

谢谢你们。

埃文斯

找到了一种行之有效的方法,最后获得了如何使用基本的LINQ select查询。询问:

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

类文件:

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

(枚举当前未使用)

希望这可以帮助其他有类似问题的人

注意:这是用于动漫类的,但是XML的结构与对应的漫画基本相同。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章