C# LINQ到XML查询不工作
查看以下XML:C# LINQ到XML查询不工作,c#,linq,linq-to-xml,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-Lan
<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>
其相应的大写和PIN值也应显示在标签中。我怎样才能做到这一点
提前问候并感谢您的查询是错误的,您的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").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
});
请提供错误详细信息“执行时出错”。是指编译错误还是运行时错误?错误消息是怎么说的?这段代码仍然给出了一个运行时错误-“空引用…”“对象引用未设置为对象的实例”。Regard我运行了它,它没有。复制并粘贴我答案中的第一个代码部分。谢谢!!!它运行良好。这是我的错。
// 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
});