如何通过 Linq 解组 XML?
好的,假设一些 XML,如:
<Result>
<Value>
<Foo>1</Foo>
<Bar>1</Bar>
</Value>
<OK>true</OK>
</Result>
和一些类:
class ResultValue
{
public int foo;
public int bar;
public ResultValue() {}
}
class Result
{
public bool ok;
public ResultValue value;
public Result() {}
}
如何创建/填充 Result 对象包括其值成员?
我真的
from x in source.Elements()
where ((int)x.Element("Value").Element("Foo") == 1)
select new Result()
{
ok = (bool) x.Element("OK"), // ok, I understand as far as this!
// what goes here, to fill .value?
};
很感激你不仅解释了我需要什么代码,还解释了“为什么”,因为我发现语法“有点”令人困惑:-P(实际上,如果你能给我指出一本不错的入门书)这,这将是一个很大的帮助,我找不到任何比平面结构更基本的内容)。
Okay, assuming some XML like:
<Result>
<Value>
<Foo>1</Foo>
<Bar>1</Bar>
</Value>
<OK>true</OK>
</Result>
And some classes:
class ResultValue
{
public int foo;
public int bar;
public ResultValue() {}
}
class Result
{
public bool ok;
public ResultValue value;
public Result() {}
}
how do I create/populate a Result object including its value member?
I got as far as
from x in source.Elements()
where ((int)x.Element("Value").Element("Foo") == 1)
select new Result()
{
ok = (bool) x.Element("OK"), // ok, I understand as far as this!
// what goes here, to fill .value?
};
I'd really appreciate you explaining not only what code I need, but "why", because I find the syntax "a bit" confusing :-P (actually, if you can point me to a decent primer on this, it would be a great help, I can't find anything that covers anything more basic than a flat structure).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过xml 序列化?可能更适合您的需求
Have you looked at xml serialisation? May be better suited to your needs