1

我有以下xml结构

<userlist>
  <user Name="something">
    <function name="funcname">
      <picture name="pictname">
         <curve name="curvename">
          <name>NAME</name>
          ...
         </curve>
      </picture>
     </function>
     <function name="function2">
     ...
     </function>
   </user>

它继续一点。我编写了一个函数来提取“函数”标签,并使用简化为的代码将它们放置在对象中:

from function in xmlDoc.Descendants("function")
select new FUNCTIONOBJECT {
 do all the rest...
 }.toList<FUNCTIONOBJECT>();

我现在正试图做到这一点,以便我只过滤给定用户的功能。所以给出了用户的名称属性。谁能告诉我如何使用 LINQ 进行这项工作?我的尝试是:

from user in xmlDoc.Descendants("user")
where user.Attribute("Name").Value == givenusername
select {
   var functions =
     from function in user.Descendants("function")
     select new FUNCTIONOBJECT {
     ... more stuff
     }.toList<FUNCTIONOBJECT>();

但这是错误的并且不起作用。所有的帮助都是好的。我对 c# 还是很陌生,仍然试图用 LINQ 进行 xml 解析。

编辑:我所拥有的更新版本仍然无法正常工作:

XDocument xmlDoc = XDocument.Load(path);

        var functionlist =
            (from user in xmlDoc.Descendants("user")
             where user.Attribute("Name").Value == username
             select(
             (from function in user.Descendants("function")
             select new Function
             {
                 name = function.Attribute("name").Value,
                 pictures =
                    (from picture in function.Descendants("picture")
                     select new Picture
                     {
                         name = picture.Attribute("name").Value,
                         layout = picture.Element("layout").Value,
                         curves =
                            (from curve in picture.Descendants("curve")
                             select new Curve
                             {
                                 name = curve.Attribute("name").Value,
                                 section = curve.Element("section").Value,
                                 run = curve.Element("run").Value,
                                 folder = curve.Element("folder").Value,
                                 drivingpoint = curve.Element("drivingpoint").Value,
                                 display = int.Parse(curve.Element("display").Value),
                                 points =
                                    (from point in curve.Descendants("point")
                                     select new Point
                                     {
                                         id = point.Element("id").Value != null ? point.Element("id").Value : string.Empty,
                                         direction = point.Element("direction").Value != null ? point.Element("direction").Value : string.Empty,
                                     }).ToList<Point>(),
                             }).ToList<Curve>(),
                     }).ToList<Picture>(),
             }).ToList<Function>(),
             ).toList();
    }
4

1 回答 1

4

只有几个语法错误。否则,内容是正确的。同时学习 C# 和 LINQ 语法(语言中的语言)有点棘手。这是更正后的代码:

from user in xmlDoc.Descendants("user")
where user.Attribute("Name").Value == givenusername
select ((from function in user.Descendants("function")  // When you do a "select something" "something" must have a value, so you can't begin with "{ var functions = ..."
         select new FUNCTIONOBJECT 
         {
             // more stuff
         }).ToList();  // You don't have to specify <FUNCTIONOBJECT> because the compiler deduce it from the context (here there a new FUNCTIONOBJECT

但是在这里,您将拥有一个List<List<FUNCTIONOBJECT>>. 为什么?因为代码中没有信息表明只有 1 个用户拥有givenusername.

如果是这种情况,只需拆分代码:

// Gets the user
var user = (from user in xmlDoc.Descendants("user")
            where user.Attribute("Name").Value == givenusername
            select user).Single();  // Get the only user that satisfy the condition (Throw an exception if no user has the given name or if multiple users have the given name)

// Gets its functions
List<FUNCTIONOBJECT> functions = (from function in user.Descendants("function")
                                  select new FUNCTIONOBJECT 
                                  {
                                      // more stuff
                                  }).ToList();  
于 2013-01-29T15:46:52.917 回答