LINQ to XML查询不起作用

时间:2014-02-12 08:57:06

标签: c# linq linq-to-xml

查看以下XML:

<Countries>
   <country name="India">
      <State name="Maharashtra" capital="Mumbai" PIN="400001"/>
      <State Name="Uttar-Pradesh" capital="Lucknow" PIN="220001"/>
   </country>
   <country name="Sri-Lanka">
      <State name="Colombo" capital="Colombo" PIN="123456"/>
      <State name="Candy" capital="Jafana" PIN="654321"/>
   </country>
 </Countries>

当我使用

public IEnumerable<CountryData> GetData()
{
     var results = from States in StockDoc.Descendants("Countries").Descendants("Country")
                      where (string)states.Attribute("Name") == "India"

                      select new CountryData
     {
        _State = (string)States.Element("Country").Element("State").Attribute("Name").Value,
        _Capital = (string)States.Element("Country").Element("State").Attribute("Capital").Value,
        _Pin= (string)States.Element("Country").Element("State").Attribute("PIN").Value
     };

      return results.ToList();
  }

这会在实施时产生错误。有什么问题?另外,请定义上述陈述的含义?

我想在下拉框中显示所有国家/地区,如

**India**
     Maharashtra
     Uttar-Pradesh
**Sri-Lanka**
     Colombo
     Candy

其大小和PIN的相应值也应显示在标签中。我怎样才能做到这一点?

提前致意并表示感谢

1 个答案:

答案 0 :(得分:1)

您的查询错误,您的xml混合使用大写和小写属性。当我遇到LINQ to XML问题时,我将查询拆分为多个步骤,这样我就可以更轻松地构建我想要的查询。

以下是您想要的:

// I've changed your xml to be consistent. Lowercase name and captial attributes
string xml = @"<Countries>
                   <country name=""India"">
                       <State name=""Maharashtra"" capital=""Mumbai"" PIN=""400001""/>
                       <State name=""Uttar-Pradesh"" capital=""Lucknow"" PIN=""220001""/>
                   </country>
                   <country name=""Sri-Lanka"">
                       <State name=""Colombo"" capital=""Colombo"" PIN=""123456""/>
                       <State name=""Candy"" capital=""Jafana"" PIN=""654321""/>
                   </country>
               </Countries>";

// Load the xml
XDocument StockDoc = XDocument.Parse(xml);

// Get states where country is "India"
IEnumerable<XElement> states = StockDoc.Root.Descendants("country")
                                       .Where(x => (string)x.Attribute("name") == "India")
                                       .Descendants("State");

// Build a new strongly typed IEnumerable<CountryData> from the xml states.
// Properties on classes in C# typically do not start with underscores. 
IEnumerable<CountryData> countryData = states.Select(y => new CountryData
                                                              {
                                                                  _State = (string)y.Attribute("name").Value,
                                                                  _Capital = (string)y.Attribute("capital").Value,
                                                                  _Pin = (string)y.Attribute("PIN").Value
                                                              });

您的初始查询出了什么问题:

var results = from States in StockDoc.Descendants("Countries").Descendants("Country")
                  where (string)states.Attribute("Name") == "India"
                  select new CountryData
 {
    _State = (string)States.Element("Country").Element("State").Attribute("Name").Value,
    _Capital = (string)States.Element("Country").Element("State").Attribute("Capital").Value,
    _Pin= (string)States.Element("Country").Element("State").Attribute("PIN").Value
 };

您在查询语法中编辑的查询:

var results = from states in StockDoc.Descendants("Countries").Elements("country")
              where (string)states.Attribute("name") == "India"
              select states.Descendants("State")
              .Select(y => new CountryData
                           {
                               _State = (string)y.Attribute("name").Value,
                               _Capital = (string)y.Attribute("capital").Value,
                               _Pin = (string)y.Attribute("PIN").Value
                           });