2014-04-16 41 views
0

我試圖從一個XML文件(即,this之一)中導入幾個不同的子項,並且我似乎無法取消該過程。我使用了一個XSD auto-gen網站(freeformatter--因爲我不熟悉xsd.exe),然後通過Xsd2code傳遞XSD爲這個列表創建一個(設計器)類,但是現在我已經迷失了方向。需要幫助將XML數據導入到C#對象

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

雖然我身邊有不少戳,我似乎無法碰到可以讓我輸入任何東西的方法,雖然我已經真的只是試圖導入數據。

簡而言之,我需要做什麼才能從前面提到的標題爲「manga.xml」的本地副本創建一個List對象?

再次,我已經嘗試了多篇其他文章,但是我覺得最好的方式就是問問某個人。

謝謝你們。

+0

它生成多個'LoadFromFile()'靜態方法。這些都沒有爲你工作? –

+0

我試過使用LoadFromFile()方法,但我似乎無法正確導入xml中的列表。例如,作爲此聲明的結果: 'var fullList = myanimelist.LoadFromFile(path);'當評估立即窗口的結果時,'?fullList.myinfo'返回整個「myinfo」段,但是'? fullList.manga'返回Count = 0,一個空列表。我有一種感覺我很接近,但接近這個錯誤的方式。 – evanjs

回答

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的結構與漫畫的結構基本相同。