C#Linq XMLファイルの読み込み例
2064 ワード
1.サンプルXMLファイル:Demo.xml
2、プログラムで以下のネーミングスペースを参照する
3、読み出しコードは以下の通りである.
infozero
lerroy
from myself
[email protected]
text
!
from others
2、プログラムで以下のネーミングスペースを参照する
using System;
using System.Linq;
using System.Xml.Linq;
3、読み出しコードは以下の通りである.
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("demo.xml");
var text = from t in doc.Descendants("conf") //
.Where(w => w.Element("to").Value.Contains('@')) //
select new
{
to = t.Element("to").Value,
froms = t.Element("from").Value,
head = t.Element("heading").Value,
body = t.Element("body").Value,
title = t.Element("title").Attribute("name").Value // attribute
};
foreach (var a in text)
{
Console.WriteLine(a.to);
Console.WriteLine(a.froms);
Console.WriteLine(a.head);
Console.WriteLine(a.body);
Console.WriteLine(a.title);
}
Console.ReadKey();
}
}