【问题标题】:Read content from the xml file in C#从 C# 中的 xml 文件中读取内容
【发布时间】:2018-07-30 08:36:25
【问题描述】:

我想在 c# 中以以下格式读取 xml 文件的内容。请告诉我

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

【问题讨论】:

  • 你试过什么?你遇到了什么问题?
  • 有很多可能性。将其读取为文本文件并逐行解析,编写基于对象的解析器,...... SO 可以帮助您解决代码中的错误,而不是帮助您完成工作。只需输入谷歌“c# .net XML parse”
  • 我不想将其转换为拖放链接中提到的对象。我只想向最终用户显示 xml 文件中的内容

标签: c# xml


【解决方案1】:

使用 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();


        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多