【问题标题】:parsing xml in c# using LINQ使用 LINQ 在 C# 中解析 xml
【发布时间】:2013-01-29 15:43:58
【问题描述】:

我有以下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();
    }

【问题讨论】:

    标签: c# xml linq parsing


    【解决方案1】:

    只有几个语法错误。否则,内容是正确的。同时学习 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&lt;List&lt;FUNCTIONOBJECT&gt;&gt;。为什么?因为代码中没有信息表明只有 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-06
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多