从C#

时间:2018-07-30 08:36:25

标签: c# xml

我想在c#中以以下格式读取内容表单xml文件。请让我知道

<Company>
  <Employee>
    <FirstName>FN</FirstName>
    <LastName>LN</LastName>
  </Employee>
 <Employee>
   <FirstName>FN1</FirstName>
   <LastName>SN1</LastName>
 </Employee>
</Company>

1 个答案:

答案 0 :(得分:0)

使用xml linq之类的代码,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication58
{
    class Program
    {
        const string FILENAME = @"C:\TEMP\TEST.XML";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<KeyValuePair<string, string>> names = doc.Descendants("Employee")
                .Select(x => new KeyValuePair<string, string>((string)x.Element("FirstName"), (string)x.Element("LastName")))
                .ToList();


        }
    }
}