简体   繁体   English

LINQ to XML获取XElement值

[英]LINQ to XML getting XElement values

I am having an issue with getting some values back from LINQ to XML query. 我在从LINQ返回一些值到XML查询时遇到问题。 I have got the XML from a SOAP web service and passed this through and parsed this into an XDocument to query 我从SOAP Web服务获取XML,并将其传递并将其解析为XDocument进行查询

The XML: 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>

The CS Code: CS代码:

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
       });

}

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. 上面的集合var为我提供了一个节点列表,但是test和test2 var却无法获取子元素。

any help appreciated! 任何帮助表示赞赏!

This is what's causing you the headache: 这是让您头疼的原因:

xmlns="http://webservices.whitespacews.com/"

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. 这将设置元素及其后代的默认名称空间-因此,您需要查找具有本地名称Collections和该名称空间的元素。 Ditto the other elements. 同上其他元素。 Fortunately, LINQ to XML makes that easy for you: 幸运的是,LINQ to XML使您轻松实现:

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
                 });

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 . 我怀疑您的test2集合具有元素,但由于无法找到DayDate元素,因此获取Value属性将导致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
                  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM