LINQ to XML获取XElement值

时间:2014-02-27 23:27:54

标签: c# xml linq linq-to-xml

我遇到了从LINQ到XML查询获取一些值的问题。我从SOAP Web服务获取XML并将其传递并解析为XDocument以进行查询

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代码:

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给出了一个节点列表,但是test和test2 var没有得到子元素。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

这就是令你头痛的原因:

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

这是为元素及其后代设置默认命名空间 - 因此您需要查找本地名称为Collections的元素和该命名空间。同样是其他元素。幸运的是,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
                 });

我怀疑您的test2集合包含元素,但因为它无法找到DayDate元素,所以Value属性将生成NullReferenceException

另请注意,您无需创建自己的阅读器等。我会将您的代码编写为:

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
                  });
相关问题