使用 LINQ 在 C# 中解析 XML
本文关键字:XML LINQ 使用 | 更新日期: 2023-09-27 17:56:35
我有以下 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();