使用循环XElements可变数量的创建XML的XDocument [英] Create XML with XDocument with a variable number of XElements using for loop

查看:172
本文介绍了使用循环XElements可变数量的创建XML的XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据e的值(每次是不同)可变数目XElements的要添加到我的XML文档。我想我需要做类似于下面我有,但下面的代码给了我6个错误的东西。它们是:

I need to add a variable number of XElements to my XML document based on the value of e(which is different each time). I think I need to do something similar to what I have below but the below code gives me 6 errors. These are:


  • 只有分配,调用,递增,递减和新对象的表达式可以作为一个语句x 2

  • 无效表达术语'为'/')'/')

  • 预计

  • Only assignment, call, increment, decrement, and new object expressions can be used as a statement x 2
  • Invalid expression term 'for'/')'/')'
  • ; expected

int e = 3;

doc.Root.Add(new XElement(ns + "LineItemList",

for(int i = 0; i <= e; i++)
{
    new XElement("ItemNumber", i.ToString());
}

));


我在做什么错了?

要提出我的问题的另一种方式,我的理解是,有我LineItemListelement里面,我需要把我的LineItem的LineItemList的声明中我LineItem元素。

To put my question another way, my understanding is that to have my LineItem elements inside my LineItemListelement, I need to put my LineItem's inside the declaration of LineItemList.

如果有人能告诉我如何明确地打开和关闭的元素,而不是将它们的开闭暗示这将有很大的帮助。

If somebody could tell me how to explicitly open and close elements, instead of them being opened and closed implicitly this would help a lot.

推荐答案

试试这个:

int e = 3;
XDocument doc = new XDocument(
          new XElement(ns + "LineItemList",
               Enumerable.Range(0, e).Select(i => new XElement("ItemNumber", i))
          ));

这篇关于使用循环XElements可变数量的创建XML的XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆