Computer/C# Updated: 2020. 7. 2. 15:42 hwaya.

XML 포멧 Element 수정 및 생성

반응형
        public bool XmlDataCheck(string strfilepath, string ElementID, string Value)
        {
            XmlDocument Dxml = new XmlDocument();
            Dxml.Load(strfilepath);

            try
            {

                // Modbus 포트번호
                XmlNode nodeModPort = Dxml.DocumentElement.SelectSingleNode("/profile/Preferences/" + ElementID);
                if (nodeModPort.Value != Value)
                {   // 수정
                    nodeModPort.InnerText = Value;
                }
                Dxml.Save(strfilepath);

                return true;
            }
            catch(Exception e)
            {
                // 생성
                XmlNode nodeModPort = Dxml.DocumentElement.SelectSingleNode("/profile/Preferences");             
                XmlNode modport = Dxml.CreateElement(ElementID);
                modport.InnerText = Value;
                nodeModPort.AppendChild(modport);
                Dxml.Save(strfilepath);
                return false;
            }
        }

 

stringfilePath : 파일위치

ElementID : 엘리멘트 아이디

Value : 값

 

 

XML 파일 포멧 로드 후, 

특정 위치의 값 확인, 원하는 값이 아니면 수정

값이 없으면, 생성

 

이런씩으로 생성됩니다.

 

자세한 설명 생략.

 

반응형

Table of Contents


    EIGHTBOX
    EIGHTBOX
    hwaya.

    programmer

    🍋일상의 소소한 순간들부터 큰 꿈까지, 호기심을 만족시킬 다양한 카테고리를 담은 블로그 입니다. 그리고, 소소한 행동에 감동하며 기뻐하고 하루하루에 감사하는 사람🌵

    Today Yesterday Total
    최신글