我真的在stackoverflow中搜索了我的问题的解决方案,但似乎我找不到它。希望有人可以帮助我!
我的问题: 我已经粘贴了一部分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();
}
方法PARSING 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 :(得分: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();
}
}
}
}