これが私がオンラインで見つけた例です: http ://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx 。LINQ to XMLコードをファイル出力と比較すると、それを理解できるはずです。
コード:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("rss",
new XAttribute("version", "2.0"),
new XElement ("channel",
new XElement("title", "RSS Channel Title"),
new XElement("description", "RSS Channel Description."),
new XElement("link", "http://aspiring-technology.com"),
new XElement("item",
new XElement("title", "First article title"),
new XElement("description", "First Article Description"),
new XElement("pubDate", DateTime.Now.ToUniversalTime()),
new XElement("guid", Guid.NewGuid())),
new XElement("item",
new XElement("title", "Second article title"),
new XElement("description", "Second Article Description"),
new XElement("pubDate", DateTime.Now.ToUniversalTime()),
new XElement("guid", Guid.NewGuid()))
)
)
);
doc.Save(@ "c:\ sample.xml");
ファイル:
<rss version="2.0">
<channel>
<title>RSS Channel Title</title>
<description>RSS Channel Description.</description>
<link>http://aspiring-technology.com</link>
<item>
<title>First article title</title>
<description>First Article Description</description>
<pubDate>2006-12-05T20:53:53.53125</pubDate>
<guid>ff7bbf19-9155-4773-913c-767bcbf09904</guid>
</item>
<item>
<title>Second article title</title>
<description>Second Article Description</description>
<pubDate>2006-12-05T20:53:53.5625</pubDate>
<guid>8a3fd5e8-b99f-49fe-8a43-7fb62d80c18c</guid>
</item>
</channel>
</rss>