[C# WPF] 다른 프로젝트에 있는 XML을 String 형태로 가져오기

728x90

 

📌 xml 파일 경로 넣어주기 

namespace Query.OSPP
{
    public class QueryPath {
        public string xmlQueryPath()

        {
            string _path = Path.Combine(HttpContext.Current.Server.MapPath("").Replace("\\Model.Service", ""), "Query.OSPP");

            return _path;
        }


    }

 

HttpContext.Current.Server.MapPath("") 로 현재 파일의 경로 추출 뒤에 자꾸  \Model.Service가 붙어서 Replace로 없애버림

 

📌 xml 파일 불러오기

- 참조에 해당 프로젝트 추가하기 

- using문에 프로젝트 추가

        public List<형이름> Get메서드이름 (string Uid)
        {
            // path 가져오기 
            QueryPath queryPath = new QueryPath();
            string tmp = queryPath.xmlQueryPath();

            // xml 연결 
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Path.Combine(tmp, "가상주소1", "xml파일명.xml"));

            // xml에서 값 가져오기 
            XmlNode ctg_node = xmlDoc.SelectSingleNode("Node태그이름");
            XmlNode name_node = xmlDoc.SelectSingleNode("Node태그이름/Query(가져올태그이름)");

 

 

📌 불러올 xml파일 작성하기

<?xml version="1.0" encoding="utf-8" ?>

<Node태그이름>
  <Query>
    쿼리문
  </Query>
</Node태그이름>

 

 

💌 Reference

https://error999.tistory.com/5

 

C# - Xml파일 제어, Xml파싱

Xml파일이나 텍스트가 있을 경우 Xml내부에 있는 값을 가져오기 위해서는 다음과 같은 방법을 쓸 수 있습니다. Xml파일 확인케이스 - Xml 속성값 가져오기 - 하위노드 리스트 가져오기 - 노드의 값

error999.tistory.com

 

728x90