处理嵌套 XML
寻找使用 Linq to XML 转换父子关系的最佳实践
在这种情况下,我有一个包含 Group[] 的 Domain 类,
我想要做的是迭代 d (域)中的 Groups[] 并创建 XElements一击。
XElement _customers = new XElement("Domains",
from c in d
orderby c.id //descending
select new XElement("Domain",
// something like--> new XElement("Groups", c.id),
// loop through d.Group and add new XElement...
new XElement("id", c.id),
new XAttribute("name", c.name),
new XElement("ismanaged", c.IsManaged.ToString()
)));
提前致谢
looking for best practice in converting a parent-child relationship using Linq to XML
In this case i have a Domain class which contains a Group[]
What I want to do is iterate over the Groups[] in d (domains) and create the XElements in one hit.
XElement _customers = new XElement("Domains",
from c in d
orderby c.id //descending
select new XElement("Domain",
// something like--> new XElement("Groups", c.id),
// loop through d.Group and add new XElement...
new XElement("id", c.id),
new XAttribute("name", c.name),
new XElement("ismanaged", c.IsManaged.ToString()
)));
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用嵌套查询来执行此操作。
假设您的类如下所示(稍微简化一下):
您可以像这样一次性创建 LINQ to XML 表示形式:
“技巧”是,如果您将 IEnumerable 作为参数之一传递给 XElement 构造函数,构造函数将枚举它并分别添加每个项目。
You can use nested queries to do this.
Assuming your classes look like this (simplifying a little bit):
You can create LINQ to XML representation in one go like this:
The "trick" is that if you pass an IEnumerable as one of the parameters to the XElement constructor, the constructor will enumerate over it and add each item separately.