1
我想要一本字典<字符串轉換,串>這個XML:轉換詞典<字符串,字符串>到XML
<root>
<key>value</key>
<key2>value2</key2>
</root>
可以這樣做使用一些花哨的LINQ?
我想要一本字典<字符串轉換,串>這個XML:轉換詞典<字符串,字符串>到XML
<root>
<key>value</key>
<key2>value2</key2>
</root>
可以這樣做使用一些花哨的LINQ?
甚至不需要得到特別花哨:
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>
沒有這樣做在手機上? :) – mrblah 2009-12-01 21:14:07