C# 使用LINQ解析c中的xml
我有以下xml结构C# 使用LINQ解析c中的xml,c#,xml,linq,parsing,C#,Xml,Linq,Parsing,我有以下xml结构 <userlist> <user Name="something"> <function name="funcname"> <picture name="pictname"> <curve name="curvename"> <name>NAME</name> ... </curve>
<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
只是一些语法错误。否则,内容是正确的。在一种语言中同时学习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
已经更新了我的版本,但仍然不起作用。你能检查一下我对原始帖子的编辑,看看我做错了什么。我已经更新了我的版本,但仍然不起作用。你能检查一下我对原始帖子的编辑,看看我做错了什么吗。