简体   繁体   English

Linq使用where子句过滤将xml读入类

[英]Linq to read xml into classes with where clause filtering

I have xml in this format. 我有这种格式的xml。 I am trying to load this data into classes, based on some selective clauses 我试图根据一些选择子句将此数据加载到类中

<Data

<CST>

<CA Type="type" Idea="Idea">
  <Var Name="Test" Value="some value" />
</CA>


<CA Type="type2" Idea="Idea2">
  <Var Name="Test2" Value="some value" />
</CA>


 <CA Type="type3" Idea="Idea3">
    <Var Name="Test" Value="some value" />
 </CA>

 </CST>

 </Data>

My code in linq to read xml is like this. 我在linq中读取xml的代码是这样的。

        IEnumerable<CA> casValues = (from s in doc.Root.Element("CST").Elements("CA")
                                 select new CA
                                 {
                                     Type = s.Attribute("Type").Value,
                                     Idea = s.Attribute("Idea").Value,
                                     Variable = (from k in s.Elements("Var")
                                                 select new Variable
                                                 {
                                                     Name = k.Attribute("Name").Value,
                                                     Value = k.Attribute("Value").Value
                                                 }).FirstOrDefault()
                                 });

This returns all the CA and their var values loaded properly. 这将返回所有已正确加载的CA及其var值。 However when I try to filter it further, because I am only interested in values with var name="test", I get an empty collection with error 但是,当我尝试进一步过滤它时,因为我只对var name =“ test”的值感兴趣,所以我得到一个空集合,并显示错误

"{"Object reference not set to an instance of an object."}"

I am using simple filtering like this. 我正在使用像这样的简单过滤。

 IEnumerable<CA> tt = casValues.Where(x => x.Variable.Name == "Test");

I am assuming this is because Variable is a class and I cannot directly use the value. 我假设这是因为Variable是一个类,并且我不能直接使用该值。 The same works when I load each element with foreach checking for my condition. 当我用foreach加载每个元素以检查我的条件时,同样的工作原理。 What I am missing with above code? 我上面的代码缺少什么?

Thanks for looking. 感谢您的光临。

Your code will throw such exception in some cases, among them, in case when Type or Idea attribute not found in the current <CA> node, or when <Var> child node is not found. 在某些情况下,其中包括当在当前<CA>节点中找不到TypeIdea属性或未找到<Var>子节点时,您的代码将引发此类异常。

One possible way to avoid exception in above mentioned occasions, instead of accessing the Value property directly, try to cast XAttribute or XElement to the expected type* : 在上述情况下,一种避免异常的可能方法是尝试将XAttributeXElement XAttribute为所需的类型*,而不是直接访问Value属性:

var casValues = (from s in doc.Root.Element("CST").Elements("CA")
                 select new CA
                            {
                                Type = (string)s.Attribute("Type"),
                                Idea = (string)s.Attribute("Idea"),
                                Variable = (from k in s.Elements("Var")
                                            select new Variable
                                                       {
                                                           Name = (string)k.Attribute("Name"),
                                                           Value = (string)k.Attribute("Value")
                                                       }).FirstOrDefault()
                            });

* ) List of available types for explicit conversion : XElement Explicit Conversion Operators , XAttribute Explicit Conversion Operators * )显式转换可用类型的列表: XElement显式转换运算符XAttribute显式转换运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM