2017-09-28 13 views
-1

私は実際に私の問題を解決するためにstackoverflowを検索しましたが、見つけられないようです。うまくいけば誰かが私を助けることができます!XML解析 - 複数のネストされた要素からデータを取得する

私の問題: 私は、XMLコードの一部を貼り付けました:

<?xml version="1.0" encoding="UTF-8"?> 
    <spocosy version="1.0" responsetime="2013-05-17 04:22:07" exec="0.241"> 
     <query-response requestid="" service="objectquery"> 
      <sport name="Cycling" enetSportCode="cy" del="no" n="0" ut="2012-12-18 10:05:23" id="30"> 
       <tournament_template name="Tour de France 1" sportFK="30" gender="male" enetID="0" del="no" n="1" ut="2012-05-22 11:20:50" id="8844"> 
        <tournament name="2011" tournament_templateFK="8844" enetSeasonID="0" del="no" locked="none" n="0" ut="2010-12-15 16:24:21" id="5830"> 
         <tournament_stage name="Tour de France" tournamentFK="5830" countryFK="5" gender="male" enetID="0" startdate="2011-07-02" enddate="2011-07-24" del="no" locked="none" n="10" ut="2011-01-19 11:15:29" id="822493"> 
          <event name="Stage 1" tournament_stageFK="822493" startdate="2011-07-02 13:05:00" eventstatusFK="0" status_type="finished" status_descFK="6" enetID="0" enetSportID="cy" del="no" locked="no" n="5" ut="2011-07-02 18:22:30" id="956268"> 
           <properties> 
            <property object="event" objectFK="956268" type="metadata" name="StartName" value="Rotterdam" del="yes" n="1" ut="2011-02-16 09:40:18" id="11468200"/> 
            <property object="event" objectFK="956268" type="metadata" name="EndName" value="Rotterdam" del="yes" n="1" ut="2011-02-16 09:40:18" id="11468201"/> 
            <property object="event" objectFK="956268" type="metadata" name="Kilometers" value="8.9" del="yes" n="1" ut="2011-02-16 09:40:18" id="11468202"/> 
            <property object="event" objectFK="956268" type="metadata" name="Number" value="1" del="no" n="0" ut="2010-12-15 16:26:30" id="11468203"/> 
            <property object="event" objectFK="956268" type="metadata" name="ParticipantType" value="athlete" del="no" n="0" ut="2010-12-15 16:26:30" id="11468204"/> 
            <property object="event" objectFK="956268" type="metadata" name="StageType" value="flat" del="no" n="1" ut="2011-04-13 12:14:12" id="11468205"/> 
            <property object="event" objectFK="956268" type="metadata" name="RaceType" value="normal" del="no" n="1" ut="2011-04-13 12:14:18" id="11468206"/> 
            <property object="event" objectFK="956268" type="metadata" name="Live" value="yes" del="no" n="0" ut="2010-12-15 16:26:30" id="11468207"/> 
            <property object="event" objectFK="956268" type="metadata" name="Kilometers" value="191.5" del="no" n="0" ut="2011-04-29 10:00:12" id="12250416"/> 
            <property object="event" objectFK="956268" type="metadata" name="StartName" value="Passage du Gois" del="no" n="0" ut="2011-04-29 10:00:12" id="12250417"/> 
            <property object="event" objectFK="956268" type="metadata" name="EndName" value="Mont des Alouettes" del="no" n="0" ut="2011-04-29 10:00:12" id="12250418"/> 
            <property object="event" objectFK="956268" type="metadata" name="Verified" value="yes" del="no" n="0" ut="2011-07-02 18:22:30" id="12630035"/> 
           </properties> 
           <event_participant number="1" participantFK="205191" eventFK="956268" del="no" n="0" ut="2011-07-02 13:02:53" id="2811827"> 
            <participant name="Peloton" gender="male" type="athlete" countryFK="652" enetID="0" enetSportID="cy" del="no" n="1" ut="2012-05-29 23:41:56" id="205191"/> 
           </event_participant> 
           <event_participant number="2" participantFK="76544" eventFK="956268" del="no" n="0" ut="2011-07-02 13:04:33" id="2811828"> 
            <participant name="Jeremy Roy" gender="male" type="athlete" countryFK="5" enetID="866" enetSportID="cy" del="no" n="3" ut="2012-05-29 22:11:33" id="76544"/> 
            <results> 
             <result event_participantsFK="2811828" result_typeFK="101" result_code="duration" value="+2:33" del="no" n="0" ut="2011-07-02 18:20:36" id="10048363"/> 
            </results> 
           </event_participant> 
           <event_participant number="3" participantFK="116303" eventFK="956268" del="no" n="0" ut="2011-07-02 13:05:01" id="2811829"> 
            <participant name="Perrig Quemeneur" gender="male" type="athlete" countryFK="5" enetID="2040" enetSportID="cy" del="no" n="1" ut="2012-05-29 23:41:56" id="116303"/> 
            <results> 
             <result event_participantsFK="2811829" result_typeFK="101" result_code="duration" value="+4:30" del="no" n="0" ut="2011-07-02 18:20:40" id="10048477"/> 
             <result event_participantsFK="2811829" result_typeFK="100" result_code="rank" value="178" del="no" n="0" ut="2011-07-02 18:20:40" id="10048478"/> 
            </results> 
           </event_participant> 
           <event_participant number="4" participantFK="167312" eventFK="956268" del="no" n="0" ut="2011-07-02 13:05:14" id="2811830"> 
            <participant name="Lieuwe Westra" gender="male" type="athlete" countryFK="9" enetID="2379" enetSportID="cy" del="no" n="1" ut="2012-05-29 23:48:32" id="167312"/> 
            <results> 
             <result event_participantsFK="2811830" result_typeFK="101" result_code="duration" value="+2:25" del="no" n="0" ut="2011-07-02 17:53:10" id="10048214"/> 
             <result event_participantsFK="2811830" result_typeFK="100" result_code="rank" value="111" del="no" n="0" ut="2011-07-02 17:53:10" id="10048215"/> 
            </results> 
           </event_participant> 

私はネストされたものなど、さまざまな要素から属性を取得したいです。私は値を持つオブジェクトを作成したい。

メイン:

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

namespace Mini_Project_Tour_De_France.TourDeFranceModel 
{ 
    public class XmlParser 
    { 
     public string fileName { get; set; } = "Cycling.xml"; 
     public List<Cyclist> participants { get; set; } = new List<Cyclist>(); 
     public string typeOfSport { get; set; } 
     public XDocument doc { get; set; } 
     public DateTime startDate { get; set; } 
     public DateTime endDate { get; set; } 


     public XmlParser() 
     { 
      doc = XDocument.Load(fileName); 
      getParticipants(); 
      getEventStartDate_EndDate(); 
      getTypeOfSport(); 
     } 

方法解析XML:ここ

public void getParticipants() 
     { 
      if (fileName != null) 
      { 
       IEnumerable<Cyclist> decendants = from c in doc.Descendants("event_participant") 
           select new Cyclist() 

           { 
            Name = c.Element("participant").Attribute("name").Value, 
            gender = c.Element("participant").Attribute("gender").Value, 
            type = c.Element("participant").Attribute("type").Value, 
            countryFK = int.Parse(c.Element("participant").Attribute("countryFK").Value), 
            enetID = int.Parse(c.Element("participant").Attribute("enetID").Value), 
            enetSportID = (c.Element("participant").Attribute("enetSportID").Value), 
            del = c.Element("participant").Attribute("del").Value, 
            n = int.Parse(c.Element("participant").Attribute("n").Value), 
            id = int.Parse(c.Element("participant").Attribute("id").Value), 
            // This ut is a dateType. It's an attribute inside the element <result> which is inside <results> which is inside <event_participant>. 
            //How can i get it? 
            ut = Convert.ToDateTime(c.Element("results").Element("result").Attribute("ut").Value), 
            // Same problem here. 
            rank = c.Element("results").Element("result").Attribute("result_code").Value 
           }; 

       participants = decendants.ToList(); 


      } 

     } 

私はそれが必要とするすべてのデータとサイクリストのオブジェクトを作成しようとしています。私は何が起こりたいのか説明しようとするのが苦労している。あなたの誰かが私が何をしようとしているのか分かりますか?そうでなければ、教えてください。私はそれをもっとうまく説明しようとします。

Cylistクラス:

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

namespace Mini_Project_Tour_De_France.TourDeFranceModel 
{ 
    public class Cyclist 
    { 
     public string Name { get; set; } 
     public string gender { get; set; } 
     public string type { get; set; } 
     public int countryFK { get; set; } 
     public string enetSportID { get; set; } 
     public int enetID { get; set; } 
     public int n { get; set; } 
     public string del { get; set; } 
     public DateTime ut { get; set; } 
     public int id { get; set; } 
     public string resultTime { get; set; } 
     public string rank { get; set; } 

     public override string ToString() { 
      return string.Format("Name: {0} gender: {1} type: {2}+ countryFK: {3} enetSportID: {4} enetID: {5} n: {6} del: {7} ut:{8} id:{9} resultTime:{10} rank: {11}" 
       ,Name,gender,type,countryFK,enetSportID,enetID,n,del,ut,id,resultTime,rank) 
      ; 
     } 

    } 
} 

ありがとう! :)

+0

私はそれは私が仕事を得ることができないサイクリストオブジェクトの最後の2つの性質だと言及するのを忘れてしまいました。それは、要素 "参加者"の内部にない属性の値を取得したいときです。 :) – Kasper

+2

1)構造化されたXMLの例を提供する2)構造化された 'XmlParser'クラスを提供する(少なくとも閉じ括弧を含む)3)Cyclistクラスを提供する4)'働くことができない 'を定義する。本質的に、誰かがあなたを助けてくれるようにしましょう。 –

答えて

0

は、次の試してみてください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 


namespace ConsoleApplication7 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      new XmlParser(FILENAME); 
     } 
    } 
    public class Cyclist 
    { 
     public string Name { get; set; } 
     public string gender { get; set; } 
     public string type { get; set; } 
     public int countryFK { get; set; } 
     public int enetID { get; set; } 
     public string enetSportID { get; set; } 
     public string del { get; set; } 
     public int n { get; set; } 
     public int id { get; set; } 
     public DateTime ut { get; set; } 
     public List<Result> results { get; set; } 
    } 
    public class Result 
    { 
     public int event_participantsFK { get; set; } 
     public int result_typeFK { get; set; } 
     public string result_code { get; set; } 
     public string value { get; set; } 
     public string del { get; set; } 
     public int n { get; set; } 
     public DateTime ut { get; set; } 
     public int id { get; set; } 
    } 
    public class XmlParser 
    { 
     public List<Cyclist> participants { get; set; } 
     public string typeOfSport { get; set; } 
     public XDocument doc { get; set; } 
     public DateTime startDate { get; set; } 
     public DateTime endDate { get; set; } 


     public XmlParser(string fileName) 
     { 
      doc = XDocument.Load(fileName); 
      getParticipants(); 
      //getEventStartDate_EndDate(); 
      //getTypeOfSport(); 
     } 
     public void getParticipants() 
     { 
      if (doc != null) 
      { 
       participants = doc.Descendants("event_participant").Select(c => new Cyclist() { 
        Name = (string)c.Element("participant").Attribute("name"), 
        gender = (string)c.Element("participant").Attribute("gender"), 
        type = (string)c.Element("participant").Attribute("type"), 
        countryFK = (int)c.Element("participant").Attribute("countryFK"), 
        enetID = (int)c.Element("participant").Attribute("enetID"), 
        enetSportID = (string)c.Element("participant").Attribute("enetSportID"), 


        del = (string)c.Element("participant").Attribute("del"), 
        n = (int)c.Element("participant").Attribute("n"), 
        id = (int)c.Element("participant").Attribute("id"), 
        ut = (DateTime)c.Element("participant").Attribute("ut"), 
        results = c.Descendants("result").Select(y => new Result() { 
         event_participantsFK = (int)y.Attribute("event_participantsFK"), 
         result_typeFK = (int)y.Attribute("result_typeFK"), 
         result_code = (string)y.Attribute("result_code"), 
         value = (string)y.Attribute("value"), 
         del = (string)y.Attribute("del"), 
         n = (int)y.Attribute("n"), 
         ut = (DateTime)y.Attribute("ut"), 
         id = (int)y.Attribute("id") 
        }).ToList() 
       }).ToList(); 

      } 

     } 
    } 

} 
関連する問題