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

本文关键字:导入 对象 数据 XML 帮助 | 更新日期: 2023-09-27 17:58:41

我试图从一个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对象?

同样,我也尝试过其他多篇文章,但我觉得最好还是问问别人。

谢谢大家。

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

找到了一个有效的方法/最终了解了如何使用基本的LINQ选择查询。查询:

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

(枚举当前未使用)

希望这能帮助其他有类似问题的

注意:这是针对anime类的,但XML的结构与manga的结构基本相同。