C# 如何使用for循环编写XML文件来附加元素

C# 如何使用for循环编写XML文件来附加元素,c#,xml,C#,Xml,我有一个文件列表框。所以我想把这个列表的项添加到XML的元素中。 但是,我在处理向XML添加项的循环时遇到了一个问题 //我的目标: // //文件1 //文件2 // // //我试图将项添加到元素 //不管看起来怎么样 文件1文件2 List drawingList=新列表(); drawingList.Add(listBox1.Items[i].ToString()); 新XDocument( 新XElement(“项目”, 新XElement(“名称”,项目名称), 新XE

我有一个文件列表框。所以我想把这个列表的项添加到XML的元素中。 但是,我在处理向XML添加项的循环时遇到了一个问题

//我的目标:
//
//文件1
//文件2
//    
//
//我试图将项添加到元素
//不管看起来怎么样
文件1文件2

List drawingList=新列表();
drawingList.Add(listBox1.Items[i].ToString());
新XDocument(
新XElement(“项目”,
新XElement(“名称”,项目名称),
新XElement(“路径”,project.Path),
新元素(“图纸”,图纸列表)
)
)
.Save(@“C:\Users\for\Desktop\abc1.cadiprj”);
);
参见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication120
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<string> drawingList = new List<string>() { "file1", "file2", "file3", "file4"};

            XDocument doc = new XDocument(
                new XElement("Project",
                    new XElement("Name", "abc"),
                    new XElement("Path", @"c:\temp\"),
                     new XElement("Drawing")
                )
           );
            XElement xDrawing = doc.Descendants("Drawing").FirstOrDefault();

            foreach (string drawing in drawingList)
            {
                xDrawing.Add(new XElement("Drawing", drawing));
            }
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序120
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
List drawingList=new List(){“file1”、“file2”、“file3”、“file4”};
XDocument doc=新XDocument(
新XElement(“项目”,
新XElement(“名称”、“abc”),
新的XElement(“路径”@“c:\temp\”,
新XElement(“图纸”)
)
);
XElement xDrawing=doc.subjects(“绘图”).FirstOrDefault();
foreach(图纸列表中的字符串图纸)
{
xDrawing.Add(新元素(“绘图”,绘图));
}
}
}
}

要实现以不同行分隔的文件,可以使用新行作为分隔符
string.Join()

List<string> drawingList = new List<string>() { "file1", "file2"};

XDocument doc = new XDocument(
    new XElement("Project",
        new XElement("Name", "abc"),
        new XElement("Path", @"c:\temp\"),
         new XElement("Drawing", string.Join("\n", drawingList.ToArray()))
    )
);
List drawingList=newlist(){“file1”、“file2”};
XDocument doc=新XDocument(
新XElement(“项目”,
新XElement(“名称”、“abc”),
新的XElement(“路径”@“c:\temp\”,
新XElement(“Drawing”,string.Join(“\n”,drawingList.ToArray()))
)
);
这将为您提供此输出

<Project>
    <Name>abd</Name>
    <Path>your path</Path>
    <Drawing>
        file1
        file2
    </Drawing>
</Project>

阿布德
你的道路
文件1
文件2
作为一个建议,如果不是固定的,我会建议一个不同的格式,这一个将使IMHO更有意义:

<Project>
    <Name>abd</Name>
    <Path>your path</Path>
    <Drawing>
        <file>file1</file>
        <file>file2</file>
    </Drawing>
</Project>

阿布德
你的道路
文件1
文件2

您的代码不是正确的do注释您缺少
drawingList
变量的声明。这是一个
列表吗?@bradbury9谢谢你的评论。Yeah List drawingList=新列表();