通过foreach循环使用linqtoxml创建XML文档

通过foreach循环使用linqtoxml创建XML文档,xml,foreach,linq-to-xml,Xml,Foreach,Linq To Xml,我正在使用LINQtoXML创建一个XML文档。以下是我所拥有的: XDocument xmlDoc = new XDocument(new XElement("requestor", new XAttribute("system", system), new XAttribute("tracking", doc.batchId + i), new XAttribute("re

我正在使用LINQtoXML创建一个XML文档。以下是我所拥有的:

XDocument xmlDoc = new XDocument(new XElement("requestor",
                    new XAttribute("system", system),
                    new XAttribute("tracking", doc.batchId + i),
                    new XAttribute("receipt", "N"),
                    new XElement("repository",
                        new XAttribute("type", "filenet"),
                        new XElement("document",
                            new XAttribute("docclass", class),
                            new XElement("file",
                                new XElement("location",
                                new XAttribute("path", doc.imagePath))),
                            new XElement("indices",
                                new XElement("index",
                                    new XAttribute("name", "CaseNum"),
                                    new XAttribute("value", doc.caseNumber)),
                                new XElement("index",
                                    new XAttribute("name", "ProvName"),
                                    new XAttribute("value", doc.name)),
                                new XElement("index",
                                    new XAttribute("name", "DOS"),
                                    new XAttribute("value", doc.date)))))));
我的问题是我需要创建多个文件节点。我有一个字符串列表,需要为列表中的每个项目创建一个文件节点。我可以在XDocument声明的中间放一个foreach循环吗?如果没有,最好的方法是什么

我尝试添加一个空白文件节点,然后添加以下内容:

foreach (string path in pathList)
                {
                    xmlDoc.Add(new XElement("location",
                                 new XAttribute("path", path)));
                }
但我不知道如何指定它应该放在文件节点下

我还想知道我完成这项任务的方式是否理想,或者是否有更理想的方式。我对LINQ相当陌生,对XML也相当陌生,所以我不知道这种方式是否会出现bug/错误等

(如果我的问题很简单,请原谅我。我对所有这些都不熟悉,这就是为什么我要求助于各位专家。我正在努力学习。谢谢!)


请求的输出:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <requestor system="CMWFS" tracking="1320530011" receipt="N">
- <repository type="filenet">
- <document docclass="abc"
- <file>
  <location path="myPath1" /> 
  </file>
- <file>
  <location path="myPath2" /> 
  </file>
- <file>
  <location path="myPath3" /> 
  </file>
- <file>
  <location path="myPath4" /> 
  </file>
- <file>
  <location path="myPath5" /> 
  </file>
- <file>
  <location path="myPath6" /> 
  </file>
- <file>
  <location path="myPath7" /> 
  </file>
- <indices>
  <index name="CaseNum" value="" /> 
  <index name="ProvName" value="" /> 
  <index name="DOS" value="7/24/2013" /> 
  </indices>
  </document>
  </repository>
  </requestor>

- 
- 

-试试这个:

//initialize list
String[] list_of_items = { "item_01", "item_02", "item_03", 
                           "item_04", "item_05", "item_06", 
                           "item_07", "item_08", "item_09", 
                           "item_10", "item_11", "item_12" };

//initialize XML-string (more readable form as no nested element declaration)
String xml_string = @"<requestor system=""CMWFS"" tracking=""1320530011"" receipt=""N"">
                         <repository type=""filenet"">
                            <document docclass=""abc"">
                               <indices>
                                  <index name=""CaseNum"" value=""""/>
                                  <index name=""ProvName"" value=""""/>
                                  <index name=""DOS"" value=""7/24/2013""/>
                               </indices>
                            </document>
                         </repository>
                      </requestor>";

//prepare XDocument
XDocument xDoc = XDocument.Parse(xml_string);

//start looping
foreach (String item in list_of_items) {
    XElement file = new XElement("file");              //file container
    XElement location = new XElement("location");      //location container
    location.Add(new XAttribute("path", item));        //adding attribute
    file.Add(location);                                //adding location to file
    xDoc.Descendants("document").First()
        .Elements("indices").First()
        .AddBeforeSelf(file);                          //adding file to document
}
Console.Write(xDoc);                                   //showing resultant
//初始化列表
字符串[]列出所有项目={“项目01”、“项目02”、“项目03”,
“第04项”、“第05项”、“第06项”,
“第07项”、“第08项”、“第09项”,
“第10项”、“第11项”、“第12项”};
//初始化XML字符串(由于没有嵌套元素声明,因此更具可读性)
字符串xml_字符串=@“
";
//准备文档
XDocument xDoc=XDocument.Parse(xml_字符串);
//开始循环
foreach(项目列表中的字符串项目){
XElement file=新XElement(“文件”);//文件容器
XElement位置=新XElement(“位置”);//位置容器
Add(新的XAttribute(“path”,item));//添加属性
file.Add(location);//将位置添加到文件
xDoc.subjects(“文档”).First()
.要素(“指数”)。第一()
.AddBeforeSelf(文件);//将文件添加到文档
}
控制台写入(xDoc)//显示结果

希望这能有所帮助。

你能提供一个请求输出的样本吗?@HamletHakobyan请原谅我的无知,但我不明白。你能解释一下或者提供一个链接到一篇文章来解释你的意思吗?谢谢