-5

i want to read content form xml file in the below format in c# . Please let me know

<Company>
  <Employee>
    <FirstName>FN</FirstName>
    <LastName>LN</LastName>
  </Employee>
 <Employee>
   <FirstName>FN1</FirstName>
   <LastName>SN1</LastName>
 </Employee>
</Company>
eth_92
  • 29
  • 1
  • 6
  • 2
    Possible duplicate of [Convert XML String to Object](https://stackoverflow.com/questions/3187444/convert-xml-string-to-object) – Drag and Drop Jul 30 '18 at 08:38
  • 3
    What have you tried? What problems did you encounter? – Adam G Jul 30 '18 at 08:38
  • 2
    Possible duplicate of [Reading XML nodes as C# objects](https://stackoverflow.com/questions/24850289/reading-xml-nodes-as-c-sharp-objects) – d219 Jul 30 '18 at 08:39
  • There are many possibilities. Read it as text file and parse it line by line, write a parser based on objects, .... . SO is there to help you with error in your code, not to do your work. Just type to google "c# .net XML parse" – greyxit Jul 30 '18 at 08:39
  • I dont want to convert it to object as mentioned in the Drag and Drop link . i just want to show the content in the xml file to end users – eth_92 Jul 30 '18 at 08:44
  • So you just want to read a text file? – gdir Jul 30 '18 at 08:47

1 Answers1

0

Use xml linq like code below :

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


        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20