Sm_data_xml.cs
XmlDocument xmlFile = new XmlDocument();
XML로드(구현완료)
public XmlNodeList xmlLoad(string file_Name) { GlobalVars.instance.setGxml_Processing_chk(false); try { xmlFile.Load( Application.dataPath + "/Sm_data_xml/" + file_Name + ".xml" ); //XmlNodeList list = xmlFile.SelectNodes("/sm_data/" + record_Name); XmlNodeList list = xmlFile.SelectNodes("/sm_data/record"); return list; } //File not found catch (System.IO.FileNotFoundException e) { Debug.Log( "Could not found!" + e.Message); return null; } //File error catch (System.Xml.XmlException e) { Debug.Log( "error!" + e.Message ); FileInfo errorLog = null; StreamWriter writer = null;
//Create or read file errorLog = new FileInfo(Application.dataPath + "/Sm_data_xml/" + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); writer = errorLog.AppendText();
//Print the error message, time writer.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")); writer.WriteLine(e.Message); writer.WriteLine();
writer.Close(); return null; } }
XML세이브 (간단하게 구현)
public void xmlSave(string file_Name) { xmlFile.AppendChild(xmlFile.CreateXmlDeclaration("1.0", "utf-8", "yes")); // Root Node XmlNode rootNode = xmlFile.CreateNode(XmlNodeType.Element, "sm_data", string.Empty); xmlFile.AppendChild(rootNode);
for (int i = 0; i < 5; i++) { //child Node XmlNode childNode = xmlFile.CreateNode(XmlNodeType.Element, "record", string.Empty); rootNode.AppendChild(childNode); XmlElement ml_level = xmlFile.CreateElement("ml_level"); ml_level.InnerText = "LV1 " + i.ToString(); childNode.AppendChild(ml_level);
XmlElement ml_1 = xmlFile.CreateElement("ml_1"); ml_1.InnerText = "ml_1 " + i.ToString(); childNode.AppendChild(ml_1);
XmlElement ml_2 = xmlFile.CreateElement("ml_2"); ml_2.InnerText = "ml_2 " + i.ToString(); childNode.AppendChild(ml_2);
XmlElement ml_3 = xmlFile.CreateElement("ml_3"); ml_3.InnerText = "ml_3 " + i.ToString(); childNode.AppendChild(ml_3);
XmlElement ml_4 = xmlFile.CreateElement("ml_4"); ml_4.InnerText = "ml_4 " + i.ToString(); childNode.AppendChild(ml_4); }
xmlFile.Save(Application.dataPath + "/Sm_data_xml/" + file_Name + ".xml"); }
쓰고 싶은 곳에서
/////////////////////////////////////////////////////////////////////////////////////// XmlNodeList list = null; list = Sm_data_xml.instance.xmlLoad("sm_mlevel_st"); // null 채크 if (list != null) { GlobalVars.instance.setGxml_Processing_chk(true); } /////////////////////////////////////////////////////////////////////////////////////// // update 문에 넣기 if( GlobalVars.instance.getGxml_Processing_chk() ) { List<Xml_sm_mlevel_st_entry> tmpList = new List<Xml_sm_mlevel_st_entry>(); foreach (XmlNode recordNode in list) { // 1. 레코드 끝까지 포문처럼 돈다 한개만 빼고 싶으면 여기서 찾아서 사용
// 2. 전체를 리스트에 넣고 싶으면 아래와 같이 코드 작성하기 Xml_sm_mlevel_st_entry entry1 = new Xml_sm_mlevel_st_entry(recordNode["ml_level"].FirstChild.InnerText, recordNode["ml_1"].FirstChild.InnerText, recordNode["ml_2"].FirstChild.InnerText, recordNode["ml_3"].FirstChild.InnerText, recordNode["ml_4"].FirstChild.InnerText); tmpList.Add(entry1); } for (int i = 0; i < list.Count; i++) { Debug.Log(tmpList[i].getStl1().ToString()); Debug.Log(tmpList[i].getStl2().ToString()); Debug.Log(tmpList[i].getStl3().ToString()); Debug.Log(tmpList[i].getStl4().ToString()); Debug.Log("----------------------"); } GlobalVars.instance.setGxml_Processing_chk(false); }
// Sm_data_xml.instance.xmlSave( "sm_mlevel_st" );
// 리스트를 통채로 xml로 만들고 불러오기 (xml 직렬화)
XmlSerializer serializer = null; FileStream stream = null; Sm_mlevel_st_entry emp = null; string fileName = string.Empty;
public void XMLSerialize(object emp, string aaa) { serializer = new XmlSerializer(typeof(Sm_mlevel_st_entry)); TextWriter writer = new StreamWriter(Application.dataPath + "/Sm_data_xml/" + "filePath" + ".xml"); serializer.Serialize(writer, emp); writer.Close(); }
public Sm_mlevel_st_entry XMLDeserialize(string filePath) { serializer = new XmlSerializer(typeof(Sm_mlevel_st_entry)); stream = new FileStream(filePath, FileMode.Open); emp = (Sm_mlevel_st_entry)serializer.Deserialize(stream); stream.Close();
return emp; }
|