繁体   English   中英

使用LINQ在c#中解析xml

[英]parsing xml in c# using LINQ

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

我现在正试图让它只过滤给定用户的功能。 所以给出了用户的name属性。 任何人都可以告诉我如何使用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#和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用户名的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();  

暂无
暂无

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

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