본문 바로가기
프로그래밍/C#

간단한 XML reader

by 체리 2021. 3. 14.
반응형

간단히 web의 xml 문서를 읽어서 필요한 정보를 찾는 방법

code는 xml 문서의 <title> tag에 있는 버전 정보만 읽어오도록 함

 

xml source: github.com/microsoft/vscode-cpptools/releases.atom

 

code

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://github.com/microsoft/vscode-cpptools/releases.atom";
            XmlReader reader = XmlReader.Create(url);
            bool checkEntry = false;
            while(reader.Read())
            {
                if (reader.IsStartElement())
                {
                    if (reader.Name == "title" && checkEntry)
                    {
                        Console.WriteLine(reader.ReadElementContentAsString());
                        break;
                    }
                    else if (reader.Name == "entry")
                    {
                        checkEntry = true;
                    }
                }
            }
            reader.Close();
            
        }
    }
}
반응형

'프로그래밍 > C#' 카테고리의 다른 글

간단한 json reader 만들기  (0) 2021.03.14

댓글