使用带有foreach循环的LINQ to XML创建XML文档

时间:2013-08-05 14:42:10

标签: xml foreach linq-to-xml

我正在使用LINQ to XML创建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很新,所以我不知道这种方式是否容易出错/错误等。

(请原谅我,如果我的问题非常简单。我是新手,这就是为什么我转向你,专家。我正在努力学习。谢谢!)


请求输出:

<?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>

1 个答案:

答案 0 :(得分:2)

试一试:

//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

希望这有帮助。