Skip to main content
 首页 » 编程设计

c#之使用 LINQ 在 C# 中解析 xml

2024年02月20日15myhome

我有如下的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 .

如果是这样,就拆分代码:

// 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();