2013-08-05 111 views
2

我使用LINQ to XML創建XML文檔。這是我有:使用LINQ to XML和foreach循環創建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))))))); 

我的問題是,我需要創建multipe文件節點。我有一個字符串列表,我需要爲列表中的每個項目創建一個文件節點。我可以把一個foreach循環放在XDocument聲明的中間嗎?如果沒有,最好的辦法是什麼?

我試圖通過添加一個空白文件節點,然後事後添加此做:

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> 
+0

您能否提供所需輸出的樣本? –

+0

@HamletHakobyan原諒我的無知,但我不明白。你能解釋一下或者提供一篇文章鏈接來解釋你的意思嗎?謝謝! – ploni

回答

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 

希望這有助於。

+0

謝謝,Cylian,工作。 – ploni