Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用for循环编写XML文件?_C#_Xml_For Loop - Fatal编程技术网

C# 如何使用for循环编写XML文件?

C# 如何使用for循环编写XML文件?,c#,xml,for-loop,C#,Xml,For Loop,我试图做一个快速的程序,创建一个非常简单的XML文件。我想创建一个XML文件,它使用“for循环”多次创建XML的一部分。当我构建时,我得到一个错误,说“无效的表达式项”for。我检查了我的括号平衡,它看起来确实是正确的 有人知道这个错误是什么意思吗 static void Main(string[] args) { XDocument myDoc = new XDocument( new XDeclaration("1.0" ,"utf-8",

我试图做一个快速的程序,创建一个非常简单的XML文件。我想创建一个XML文件,它使用“for循环”多次创建XML的一部分。当我构建时,我得到一个错误,说“无效的表达式项”for。我检查了我的括号平衡,它看起来确实是正确的

有人知道这个错误是什么意思吗

    static void Main(string[] args)
    { 
        XDocument myDoc = new XDocument(
        new XDeclaration("1.0" ,"utf-8","yes"),
        new XElement("Start"),

        for(int i = 0; i <= 5; i++)
        {
                         new XElement("Entry", 
                             new XAttribute("Address", "0123"),
                             new XAttribute("default", "0"),

                        new XElement("Descripion", "here is the description"),

                        new XElement("Data", "Data goes here ")

                );

            }
        );

        }
static void Main(字符串[]args)
{ 
XDocument myDoc=新XDocument(
新的XDE声明(“1.0”、“utf-8”、“是”),
新XElement(“开始”),

对于(int i=0;i),您现在试图在语句的中间使用<代码> < /COD>循环。这不起作用。

选项:

  • 使用
    Enumerable.Range
    创建一个值范围,然后使用
    选择
    转换该值范围:

    XDocument myDoc = new XDocument(
        new XDeclaration("1.0" ,"utf-8","yes"),
        // Note that the extra elements are *within* the Start element
        new XElement("Start",
            Enumerable.Range(0, 6)
                      .Select(i => 
                           new XElement("Entry", 
                               new XAttribute("Address", "0123"),
                               new XAttribute("default", "0"),
                               new XElement("Descripion", "here is the description"),
                               new XElement("Data", "Data goes here ")))
    );
    
  • 首先创建文档,并在循环中添加元素

    XDocument myDoc = new XDocument(
        new XDeclaration("1.0" ,"utf-8","yes"),
        new XElement("Start"));
    for (int i = 0; i <= 5; i++) 
    {
        myDoc.Root.Add(new XElement("Entry",
                           new XAttribute("Address", "0123"),
                           new XAttribute("default", "0"),
                           new XElement("Descripion", "here is the description"),
                           new XElement("Data", "Data goes here "));
    }
    
    XDocument myDoc=新XDocument(
    新的XDE声明(“1.0”、“utf-8”、“是”),
    新XElement(“开始”);
    
    对于(inti=0;i使用XML序列化。使用IMO更容易


    这可能是问题所在,但我是否可以使用其他方法?这肯定是问题所在,但这并不适用于编译。您可能想了解c#和XElement中参数的工作原理。这是Jon Skeet爵士在那里说的!当我使用此方法时,我收到一个运行时错误,说这会创建一个结构不正确的文档。