■ サンプル
Program.cs
using System; using System.Xml.Linq; namespace xmlDemo1 { class Program { static void Main(string[] args) { XDocument xdoc = XDocument.Load(@"C:\sample.xml"); foreach (var xbook in xdoc.Root.Elements()) { XElement xname = xbook.Element("title"); Console.WriteLine(xname.Value); } Console.Read(); } } }
sample.xml
<?xml version="1.0" encoding="utf-8" ?> <books> <book isbn="978-4873112213"> <title>XML Hacks</title> <publisher>Oreilly</publisher> <authers> <auther>Michael Fitzgerald</auther> </authers> <price>3360</price> </book> <book isbn="978-0596516109"> <title>C# CookBook</title> <publisher>Oreilly</publisher> <authers> <auther>Jay Hilyard</auther> <auther>Stephen Teilhet</auther> </authers> <price>5200</price> </book> <book isbn="978-0596527730"> <title>C# 3.0 Design Patterns</title> <publisher>Oreilly</publisher> <authers> <auther>Judith Bishop</auther> </authers> <price>4200</price> </book> <book isbn="978-1933988160"> <title>Linq in Action</title> <publisher>Manning Pubns</publisher> <authers> <auther>Fabrice Marguerie</auther> <auther>Steve Eichert</auther> <auther>Jim Wooley</auther> </authers> <price>4630</price> </book> <book isbn="978-0596100506"> <title>Xml Pocket Reference</title> <publisher>Oreilly</publisher> <authers> <auther>Simon St. Laurent</auther> <auther>Michael Fitzgerald</auther> </authers> <price>1160</price> </book> </books>
出力結果
<title>タグの値を表示されるXML Hacks C# CookBook C# 3.0 Design Patterns Linq in Action Xml Pocket Reference
関連記事
XML関連
C# でXMLシリアライズ・デシリアライズ ~ XmlSerializerクラス ~https://blogs.yahoo.co.jp/dk521123/22166698.html
C# でXMLシリアライズ・デシリアライズ ~ DataContractSerializerクラス ~
https://blogs.yahoo.co.jp/dk521123/37973448.html
XML Serializeで、xmlnsなどの余計なタグを削除する
https://blogs.yahoo.co.jp/dk521123/24459035.html