こんにちは、以下のようなXMLがあるとします
XML
1<?xml version="1.0" encoding="UTF-8"?> 2<Message> 3 <key Name = "Success" value = "成功" /> 4 <key Name = "Fail" value = "失敗" /> 5</Message>
このようなXMLファイルをC#で読み込んでkey Nameで検索してValueの値を表示する場合は、C#でどのように書いたらいいでしょうか?
一応Messageまでは取れるのですが、内部のSuccessやFailにアクセスする方法が判りません。
よろしくお願いいたします
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Linq; 7using System.Xml.Linq; 8 9namespace ConsoleApp1 10{ 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 XElement xml = XElement.Load(@"Test.xml"); 16 17 //Messageのタグ内の情報を取得する 18 IEnumerable<XElement> infos = from item in xml.Elements("Message") select item; // ここまではとれている(currentがnull) 19 // ここから下の(infoの)出力がない 20 foreach (XElement info in infos) 21 { 22 Console.Write(info.Element("Success").Value + @","); 23 Console.Write(info.Element("Fail").Value + @","); 24 } 25 Console.ReadKey(); 26 } 27 } 28}
回答1件
あなたの回答
tips
プレビュー