我有以下XML(缩减版)文件:
<Service z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<routes>
<Route z:Id="i4">
<timetables>
<Timetable z:Id="i8">
<timetableId>11061</timetableId>
</Timetable>
<Timetable z:Id="i8">
<timetableId>11062</timetableId>
</Timetable>
</timetables>
</Route>
</routes>
</Service>
我能够获得第一个ID:11061,但我希望得到第二个ID,在真实文件中会有其他几个。但是我假设一旦我得到两个,它就会超过2个。
XDocument doc = XDocument.Load("timetableTest.xml");
XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";
var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
select new RootContainer
{
Services = (from s in n.Elements(ns + "timetables")//.Elements(ns + "clients")
// where n.Elements(ns + "Service") != null
select new Services
{
ServiceName = s.Element(ns + "Timetable").Element(ns + "timetableId").Value,
//serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
// ServiceId = s.Element(ns + "serviceId").Value
}).ToList()
}).Single();
listServices.ItemsSource = routeNames.Services;
为了获得多个时间表ID,我需要改变什么?
更新:如何使用两条路线做同样的事情?只需重新查看原始的xml提要。
<Service z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<routes>
<Route z:Id="i4">
<timetables>
<Timetable z:Id="i8">
<timetableId>11061</timetableId>
</Timetable>
<Timetable z:Id="i8">
<timetableId>11062</timetableId>
</Timetable>
</timetables>
</Route>
<Route z:Id="i4">
<timetables>
<Timetable z:Id="i8">
<timetableId>11061</timetableId>
</Timetable>
<Timetable z:Id="i8">
<timetableId>11062</timetableId>
</Timetable>
</timetables>
</Route>
</routes>
</Service>
答案 0 :(得分:0)
您需要一个化合物from clause来选择许多时间表:
XDocument doc = XDocument.Load("timetableTest.xml");
XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";
var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
select new
{
Services = (from s in n.Elements(ns + "timetables")//.Elements(ns + "clients")
from t in s.Descendants(ns + "Timetable") // where n.Elements(ns + "Service") != null
select new
{
ServiceName = t.Element(ns + "timetableId").Value,
//serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
// ServiceId = s.Element(ns + "serviceId").Value
}).ToList()
}).Single();
listServices.ItemsSource = routeNames.Services;
答案 1 :(得分:0)
在内部for循环RootContainer类
中使用后代Services = (from s in n.Elements(ns + "timetables").Descendants(ns +"Timetable")
并直接在元素
中访问ServiceName = s.Element(ns + "timetableId").Value,
完整代码
var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
select new RootContainer
{
Services = (from s in n.Elements(ns + "timetables").Descendants(ns +"Timetable")
// where n.Elements(ns + "Service") != null
select new Services
{
ServiceName = s.Element(ns + "timetableId").Value,
//serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
// ServiceId = s.Element(ns + "serviceId").Value
}).ToList()
}).Single();