我正在使用以下 linq 查询来处理 XML 文件。
XElement rootElement = XElement.Load(@"test.xml");
int StyCode;
var lv1s = from lv1 in rootElement.Descendants("Class")
where lv1.Attribute("Code").Value.Equals("002")
select new
{
Children = from ltd in lv1.Descendants("Subject")
where ltd.Attribute("Course").Value.Equals("Math")
select new
{
****//In This below section need result on condition based... ****
if(StyCode=0)
{
Children1 = ltd.Attribute("AllTeachers").Value.Equals("Y") ? true : false
}
else
{
Children2 = ltd.Attribute("SpeciaGuest").Value.Equals("Y") ? true : ltd.Elements("Topic").Attributes("Code").Where(x => x.Value.Equals("1")).FirstOrDefault() != null ? true : false
}
}
};
下面是我的 XML 结构..
<?xml version="1.0" encoding="utf-8" ?>
<Document>
<Class Code="001">
<Subject Course="Math" AllTeachers='N' SpeciaGuest='N'></Subject>
<Subject Course="Engish" AllTeachers='Y' SpeciaGuest='Y'></Subject>
<Subject Course="History" AllTeachers='Y' SpeciaGuest='Y'></Subject>
</Class>
<Class Code="002">
<Subject Course="Math" AllTeachers='Y' SpeciaGuest='N'>
<Topic Code="1">LAW1</Topic>
<Topic Code="2">
LAW2
</Topic>
<Topic Code="3">
LAW3</Topic>
</Subject>
<Subject Course="Engish" AllTeachers='Y' SpeciaGuest='Y'></Subject>
<Subject Course="History" AllTeachers='Y' SpeciaGuest='Y'></Subject>
</Class>
</Document>
请告诉我,我如何使用条件来选择多选结果。也让我知道在这个 linq 查询中使用 resharper 会很好吗?