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

时间:2014-04-16 00:17:18

标签: c# xml xsd

我试图从XML文件中导入几个不同的孩子(即this一个),我似乎无法让程序失效。我使用了一个XSD自动生成站点(自由格式化 - 因为我不熟悉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>

Resulting Class

虽然我已经玩了很多,但我似乎无法找到一种方法可以让我导入任何东西,尽管我真的只是在尝试导入数据。

简而言之,我需要做什么才能从前面提到的名为&#34; manga.xml&#34;?的列表的本地副本创建一个List对象。

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

谢谢你们。

1 个答案:

答案 0 :(得分:0)

找到一个有效/终于如何使用基本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; }
    }
}

(枚举目前尚未使用)

希望这有助于其他有类似问题的人

注意:这是针对动画类的,但XML的结构基本上与它的漫画相同。