public static void ItemsToXml(List <CPacketItem> items, String xmlFile, CStructInfo info) { XmlDocument xDoc = new XmlDocument(); //List<CPacketItem> items = cstr.Items; XmlElement xRoot = xDoc.CreateElement("Packet"); xDoc.AppendChild(xRoot); #region addInfo XmlElement xInfos = XmlAdder.Element(xDoc, "Infos", xRoot); XmlElement xInfo; xInfo = XmlAdder.Element(xDoc, "Info", xInfos); XmlAdder.Attribute(xDoc, "Comment", info.Comment, xInfos); xInfo = XmlAdder.Element(xDoc, "Info", xInfos); XmlAdder.Attribute(xDoc, "LastModified", info.LastModified.ToString(), xInfos); #endregion XmlElement xItems = XmlAdder.Element(xDoc, "Items", xRoot); for (int i = 0; i < items.Count; i++) { XmlElement item = xDoc.CreateElement("Item"); XmlAdder.Attribute(xDoc, "Name", items[i].Name, item); XmlAdder.Attribute(xDoc, "Type", items[i].TypeString, item); XmlAdder.Attribute(xDoc, "IsSwap", items[i].IsSwap.ToString(), item); int size = items[i].Length; if (items[i].Function.Exists) { XmlElement func = XmlAdder.Element(xDoc, "Function", item); XmlAdder.Attribute(xDoc, "Name", items[i].Function.Name, func); for (int argc = 0; argc < items[i].Function.Args.Length; argc++) { XmlAdder.Element(xDoc, "Arg", items[i].Function.Args[argc].ToString(), func); } } else if (items[i].InitValues.Length > 0) { if (size == 0) { size = items[i].InitValues.Length; } for (int itemCount = 0; itemCount < size; itemCount++) { XmlAdder.Element(xDoc, "InitValue", items[i].InitValues[itemCount].ToString(), item); } } XmlAdder.Attribute(xDoc, "Size", size.ToString(), item); xItems.AppendChild(item); } xDoc.Save(xmlFile); }
public void SaveFile() { //File.WriteAllText(_saveFile, T_Msg.Text); _ns.NativeText = T_Msg.Text;//.setNativeText(ss); //_ns.Items = StructXMLParser.CodeToItems(T_Msg.Text, _ns); if (_ns.IsDynamicPacket == false) { _ns.MakePacket(_swapWhenMakePacket); } CStructInfo info = new CStructInfo(T_Comment.Text, DateTime.Now); StructXMLParser.ItemsToXml(_ns.Items, _xmlFile, info); }
public static List <CPacketItem> XmlToItems(String xmlFile, CStructInfo info = null) { List <CPacketItem> items = new List <CPacketItem>(); XmlDocument xDoc = new XmlDocument(); xDoc.Load(xmlFile); XmlNodeList xNodeList; if (info != null) { xNodeList = xDoc.SelectNodes("//Packet//Infos//Info"); for (int i = 0; i < xNodeList.Count; i++) { String name = xNodeList.Item(i).Attributes["Name"].Value; String value = xNodeList.Item(i).InnerText; switch (name) { case "Comment": info.Comment = value; break; case "LastModified": info.SetModified(value); break; } } } xNodeList = xDoc.SelectNodes("//Packet//Items//Item"); for (int i = 0; i < xNodeList.Count; i++) { String name = xNodeList.Item(i).Attributes["Name"].Value; String type = xNodeList.Item(i).Attributes["Type"].Value; bool isSwap = false; try { isSwap = xNodeList.Item(i).Attributes["IsSwap"].Value.ToLower().Equals("true"); } catch { } int size = 0; try{ size = Convert.ToInt32(xNodeList.Item(i).Attributes["Size"].Value); }catch {} XmlNodeList initValues = XmlGetter.Children(xNodeList.Item(i), "InitValue"); // xNodeList.Item(i).ChildNodes; CPacketItem item = new CPacketItem(name, type, size, "0", null); //default if (initValues.Count > 1) { if (size == 0) { size = initValues.Count; //size가 지정되지 않았으면 초기설정개수와 동일하게 맞춘다. } List <String> initObjValues = new List <String>(); if (initValues.Count <= size) //정해진 배열크기보다 초기값설정이 적거나 일치할 때 { for (int initCount = 0; initCount < initValues.Count; initCount++) //초기설정을 넣는다. { if (initValues.Item(initCount).Name.Equals("InitValue")) { initObjValues.Add(initValues.Item(initCount).InnerText); } } for (int initCount = initValues.Count; initCount < size; initCount++)//초기설정이 모자라면 0으로 채운다. { if (initValues.Item(initCount).Name.Equals("InitValue")) { initObjValues.Add("0"); } } } else//초기설정값이 더 많을 때.. { for (int initCount = 0; initCount < size; initCount++) //초기설정을 size에서 정해진 만큼만 넣는다. { if (initValues.Item(initCount).Name.Equals("InitValue")) { initObjValues.Add(initValues.Item(initCount).InnerText); } } } item = new CPacketItem(name, type, size, initObjValues.ToArray()); } XmlNode function = XmlGetter.Child(xNodeList.Item(i), "Function");// xNodeList.Item(i).ChildNodes; if (function != null) { FunctionInfo func = new FunctionInfo(); if (initValues.Item(0).Name.Equals("Function")) { XmlNodeList args = initValues.Item(0).ChildNodes; List <String> argList = new List <string>(); String funcName = initValues.Item(0).Attributes["Name"].Value; for (int argc = 0; argc < args.Count; argc++) { argList.Add(args[argc].InnerText); } String[] argArray; if (argList.Count > 0) { argArray = argList.ToArray(); } else { argArray = null; } if (FunctionsList.ContainsKey(funcName)) { func = new FunctionInfo(FunctionsList[funcName], argArray); } else { func = new FunctionInfo(funcName, null, argArray); // } } if (func.Exists) { item = new CPacketItem(name, type, size, func, null); } else { String initObj = initValues.Item(0).InnerText; item = new CPacketItem(name, type, size, initObj, null); } } item.IsSwap = isSwap; items.Add(item); } return(items); }
public static void CodeToXml(String code, String xmlFile, CStructInfo info = null) { List <CPacketItem> items = ScenarioXmlParser.CodeToItems(code); ScenarioXmlParser.ItemsToXml(items, xmlFile, info); }
public static String XmlToCode(String xmlFile, CStructInfo info = null) { List <CPacketItem> list = XmlToItems(xmlFile, info); return(ItemsToCode(list)); }