2014-02-12 48 views
0

有以下XML来看看:的LINQ to 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(); 
    } 

这在实施产生错误。什么是问题?另外,请定义以上声明的含义?

我想显示在dropdownbox所有国家喜欢 -

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

而且其对应的值作为资本和PIN应在标签中显示。我怎样才能做到这一点?

的问候和感谢事先

+0

请提供错误详细信息 –

+1

“执行错误”。意味着编译错误或运行时错误?错误信息是什么意思? – doctorlove

+0

请寄xml请 –

回答

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

有什么错你inital查询:

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

这段代码还有一个运行时错误 - “空引用....”“对象引用没有设置为对象的一个​​实例。”问候 – user3248647

+0

我跑了它,它没有。将第一个代码段复制并粘贴到我的答案中。 –

+0

谢谢!它运作良好。有我的错。 – user3248647