2009-12-01 154 views

回答

4

甚至不需要得到特別花哨:

var xdoc = new XDocument(new XElement("root", 
     dictionary.Select(entry => new XElement(entry.Key, entry.Value)))); 

完整的示例:

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

class Test 
{ 
    static void Main() 
    { 
     var dictionary = new Dictionary<string, string> 
     { 
      { "key", "value" }, 
      { "key2", "value2" } 
     }; 

     var xdoc = new XDocument(new XElement("root", 
      dictionary.Select(entry => new XElement(entry.Key, entry.Value)))); 

     Console.WriteLine(xdoc); 
    } 
} 

輸出:

<root> 
    <key>value</key> 
    <key2>value2</key2> 
</root> 
+0

沒有這樣做在手機上? :) – mrblah 2009-12-01 21:14:07

相關問題