LINQ到XML得到的XElement值 [英] LINQ to XML getting XElement values
问题描述
我有得到一些值从LINQ返回XML查询的问题。我已得到SOAP Web服务的XML和穿过这一点,并解析成一个XDocument此查询
I am having an issue with getting some values back from LINQ to XML query. I have got the XML from a SOAP web service and passed this through and parsed this into an XDocument to query
中的XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCollectionByUprnAndDateResponse xmlns=\"http://webservices.whitespacews.com/\">
<GetCollectionByUprnAndDateResult>
<ErrorCode>0</ErrorCode>
<ErrorDescription>Success</ErrorDescription>
<SuccessFlag>true</SuccessFlag>
<Collections>
<Collection>
<Service>Garden Waste Collection Service</Service>
<Round>RR3 GDN</Round>
<Schedule>TueFort2</Schedule>
<Day>Tuesday</Day>
<Date>01/04/2014</Date>
</Collection>
<Collection>
<Service>Recycling Collection Service</Service>
<Round>RR8 REC</Round>
<Schedule>TueFort2</Schedule>
<Day>Tuesday</Day>
<Date>01/04/2014</Date>
</Collection>
<Collection>
<Service>Refuse Collection Service</Service>
<Round>RR8 REF</Round>
<Schedule>TueFort1</Schedule>
<Day>Tuesday</Day>
<Date>08/04/2014</Date>
</Collection>
<Collection>
<Service>Garden Waste Collection Service</Service>
<Round>RR3 GDN</Round>
<Schedule>TueFort2</Schedule>
<Day>Tuesday</Day>
<Date>15/04/2014</Date>
</Collection>
<Collection>
<Service>Recycling Collection Service</Service>
<Round>RR8 REC</Round>
<Schedule>TueFort2</Schedule>
<Day>Tuesday</Day>
<Date>15/04/2014</Date>
</Collection>
<Collection>
<Service>Refuse Collection Service</Service>
<Round>RR8 REF</Round>
<Schedule>TueFort1</Schedule>
<Day>Tuesday</Day>
<Date>22/04/2014</Date>
</Collection>
</Collections>
</GetCollectionByUprnAndDateResult>
</GetCollectionByUprnAndDateResponse>
</soap:Body>
</soap:Envelope>
在CS代码:
The CS Code:
using (var reader = new StreamReader(response.GetResponseStream()))
{
string xmlString = reader.ReadToEnd();
XDocument xmlDoc = XDocument.Parse(xmlString);
var collections = (from p in xmlDoc.Descendants()
where p.Name.LocalName == "Collection"
select p);
var test = xmlDoc.Descendants("Collection").Select(
x => new
{
day = x.Element("Day").Value,
date = x.Element("Date").Value
});
Debug.WriteLine("STOP HERE");
var test2 = (from p in xmlDoc.Descendants()
where p.Name.LocalName == "Collection"
select new{
day = p.Element("Day").Value,
date = p.Element("Date").Value
});
}
上面的集合VAR给我的节点列表,然而,测试和test2的VAR的没有得到让我的子元素。
The collections var above gives me a list of the nodes, however the test and test2 var's does not get get me the child elements.
任何帮助,不胜感激!
推荐答案
这是什么导致你头痛:
xmlns="http://webservices.whitespacews.com/"
这是设置默认名称空间的元素及其后代 - 所以你需要寻找与正宗的本地名称
和命名空间的元素。同上的其他元素。幸运的是,LINQ到XML使这便于您:
That's setting the default namespace for the element and its descendants - so you need to look for elements with a local name of Collections
and that namespace. Ditto the other elements. Fortunately, LINQ to XML makes that easy for you:
XNamespace ns = "http://webservices.whitespacews.com/";
...
var test = xmlDoc.Descendants(ns + "Collection")
.Select(x => new
{
day = x.Element(ns + "Day").Value,
date = x.Element(ns + "Date").Value
});
我怀疑你的测试2
集合的将的有元素,而是因为它不能找到天
和日期
元素,所以取了值
属性将导致的NullReferenceException
。
I suspect your test2
collection would have elements, but because it won't be able to find the Day
and Date
elements, so fetching the Value
property will result in a NullReferenceException
.
另外请注意,没有必要为你打造你自己的阅读器等,我会写你的代码为:
Also note that there's no need for you to create your own reader etc. I would write your code as:
XNamespace ns = "http://webservices.whitespacews.com/";
XDocument doc;
using (var stream = response.GetResponseStream())
{
doc = XDocument.Load(stream);
}
var query = xmlDoc.Descendants(ns + "Collection")
.Select(x => new
{
Day = x.Element(ns + "Day").Value,
Date = x.Element(ns + "Date").Value
});
这篇关于LINQ到XML得到的XElement值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!