-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)
;
}
}
}
謝謝! :)
我忘了提及,這只是自行車對象的最後2個屬性,我不能去上班。這是當我想獲得不屬於元素「參與者」的屬性的值。 :) – Kasper
1)提供一個結構良好的XML示例2)提供一個結構良好的'XmlParser'類(至少包括閉括號)3)提供'Cyclist'類4)define'無法工作'。實質上,讓別人更容易幫助你。 –