Advertisement
kiril_dishliev

Xml Text Reader

Nov 14th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8.  
  9. namespace ConsoleApplication2
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string name = string.Empty;
  16.             string capital = string.Empty;
  17.             using (XmlTextReader xmlReader = new XmlTextReader(@"C:\Users\DISHLIEV\Desktop\country.xml"))
  18.             {
  19.                 while (xmlReader.Read())
  20.                 {
  21.                     xmlReader.WhitespaceHandling = WhitespaceHandling.None;
  22.  
  23.                     if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name.Equals("countries"))
  24.                         break;
  25.  
  26.                     if (xmlReader.NodeType == XmlNodeType.Element)
  27.                         switch (xmlReader.Name)
  28.                         {
  29.                             case "country":
  30.                                 XDocument xDoc = XDocument.Parse("<root>" + xmlReader.ReadInnerXml() + "</root>");
  31.                                 name = xDoc.Root.Elements("name").FirstOrDefault().Value;
  32.                                 capital = xDoc.Root.Elements("capital").FirstOrDefault().Value;
  33.                                 break;
  34.                         }
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement