[Unity 기초] XML로 데이터 저장

배근철·2022년 11월 14일

Unity 기초

목록 보기
10/10

✍ XML로 데이터 저장하기


XmlDocument Document = new XmlDocument(); // XMl파일생성
XmlElement xmlEl = Document.CreateElement("DB"); // 요소 생성
Document.AppendChild(xmlEl); // 요소를 XML문서에 첨부


XmlElement elementSet = Document.CreateElement("item"); // 요소 생성
elementSet.SetAttribute("Name", itemName); // 요소에 속성을 저장함 (문자열)

Document.AppendChild(xmlEl); // XML파일에 요소를 저장
Document.Save("저장경로"); // 저장경로에 XML파일을 저장한다.

✍ XML로 저장된 데이터 불러오기


if(!System.IO.File.Exists("저장경로"))
	return; // 저장경로에 존재하지않으면 리턴한다.
    
XmlDocument xmlDoc = new XmlDocument(); // XMl파일생성
xmlDoc.Load("저장경로"); // 경로에 있는 XML파일을 로드
XmlElement xmlEl = xmlDoc["DB"]; // 속성 DB에 접속

0개의 댓글