X元素=>在运行时添加子节点

发布于 2024-12-22 09:15:19 字数 1134 浏览 0 评论 0原文

因此,我们假设这就是我想要实现的目标:

<root>
  <name>AAAA</name>
  <last>BBBB</last>
  <children>
     <child>
        <name>XXX</name>
        <last>TTT</last>
     </child>
     <child>
        <name>OOO</name>
        <last>PPP</last>
     </child>
   </children>
</root>

不确定使用 XElement 是否是最简单的方法
但这是我到目前为止所拥有的:

 XElement x = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"));

现在我必须根据我拥有的一些数据添加“孩子”。
可能有 1,2,3,4 ...

所以我需要迭代我的列表以获取每个子节点

foreach (Children c in family)
{
    x.Add(new XElement("child", 
              new XElement("name", "XXX"),
              new XElement("last", "TTT")); 
} 

问题:

这样做我将丢失“CHILDREN Parent node”。 如果我只是在 foreach 之前添加它,它将被渲染为一个封闭节点

<children/>

,而这不是我们想要的。

问题:

如何向第一部分添加父节点以及我的列表中的尽可能多的节点?

So let's assume this is what i want to achieve:

<root>
  <name>AAAA</name>
  <last>BBBB</last>
  <children>
     <child>
        <name>XXX</name>
        <last>TTT</last>
     </child>
     <child>
        <name>OOO</name>
        <last>PPP</last>
     </child>
   </children>
</root>

Not sure if using XElement is the simplest way
but this is what I have so far:

 XElement x = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"));

Now I have to add the "children" based on some data i have.
There could be 1,2,3,4 ...

so I need to iterate thru my list to get every single child

foreach (Children c in family)
{
    x.Add(new XElement("child", 
              new XElement("name", "XXX"),
              new XElement("last", "TTT")); 
} 

PROBLEM:

Doing this way I will be missing the "CHILDREN Parent node".
If I just add it before the foreach, it will be rendered as a closed node

<children/>

and that's NOT what we want.

QUESTION:

How can I add to the 1st part a parent node and as many as my list has?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-12-29 09:15:19

试试这个:

var x = new XElement("root",
             new XElement("name", "AAA"),
             new XElement("last", "BBB"),
             new XElement("children",
                 from c in family
                 select new XElement("child",
                             new XElement("name", "XXX"),
                             new XElement("last", "TTT")
                        )
             )
        );

Try this:

var x = new XElement("root",
             new XElement("name", "AAA"),
             new XElement("last", "BBB"),
             new XElement("children",
                 from c in family
                 select new XElement("child",
                             new XElement("name", "XXX"),
                             new XElement("last", "TTT")
                        )
             )
        );
梦年海沫深 2024-12-29 09:15:19
 XElement root = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"));

XElement children = new XElement("children");

foreach (Children c in family)
{
    children.Add(new XElement("child", 
              new XElement("name", c.Name),
              new XElement("last", c.Last)); 
}
root.Add(children);
 XElement root = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"));

XElement children = new XElement("children");

foreach (Children c in family)
{
    children.Add(new XElement("child", 
              new XElement("name", c.Name),
              new XElement("last", c.Last)); 
}
root.Add(children);
烦人精 2024-12-29 09:15:19
var children = new XElement("children");
XElement x = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"),
                  children);

foreach (Children c in family)
{
    children.Add(new XElement("child", 
              new XElement("name", "XXX"),
              new XElement("last", "TTT")); 
} 
var children = new XElement("children");
XElement x = new XElement("root",
                  new XElement("name", "AAA"),
                  new XElement("last", "BBB"),
                  children);

foreach (Children c in family)
{
    children.Add(new XElement("child", 
              new XElement("name", "XXX"),
              new XElement("last", "TTT")); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文