/// <summary> /// 将一个值保存至某个Xml中 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="_XDoc">文档</param> /// <param name="uri">路径</param> /// <param name="nodeNames">文档中路径</param> /// <param name="value"></param> public static void WriteValueToXML <T>(this XDocument _XDoc, string uri, string[] nodeNames, T value) { if (_XDoc.Root == null) { _XDoc.Add(new XElement(Path.GetFileNameWithoutExtension(uri))); } XElement _Root = _XDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } XElement newRoot = new XElement(_Root.Name); XElement parent = _Root.Parent; _Root.Remove(); parent.Add(newRoot); newRoot.Value = StringEx.ConverToString(value); _XDoc.Save(uri); }
/// <summary> /// 将值加入XML /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xDoc"></param> /// <param name="nodeNames"></param> /// <param name="value"></param> public static XDocument WriteValue <T>(this XDocument xDoc, string[] nodeNames, T value) { if (xDoc.Root == null) { xDoc.Add(new XElement("Root")); } XElement _Root = xDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } XElement newRoot = new XElement(_Root.Name); XElement parent = _Root.Parent; _Root.Remove(); parent.Add(newRoot); newRoot.Value = StringEx.ConverToString(value); return(xDoc); }
/// <summary> /// 向指定XML输入数组内容 /// </summary> /// <param name="uri">文件的位置</param> /// <param name="nodeNames">上层的结点名</param> /// <param name="Tag">要加入的子结点们的名字</param> /// <param name="arr">要加入的子节点们的内容</param> public static void WriteListToXML <T>(string uri, string[] nodeNames, List <T> arr, string tag = "item") { XDocument _XDoc = XDocument.Load(uri); if (_XDoc == null) { _XDoc.Save(uri); } XElement _Root = _XDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } _Root.RemoveAll(); foreach (T ob in arr) { _Root.Add(new XElement(tag, StringEx.ConverToString(ob))); } _XDoc.Save(uri); }
/// <summary> /// 向指定XML输入Dictionary内容 /// </summary> /// <param name="uri">文件的位置</param> /// <param name="nodeNames">上层的结点名</param> /// <param name="_dic">要写入XML的Dictionary结构体</param> public static void WriteDictionaryToXML <T>(string uri, string[] nodeNames, Dictionary <string, T> _dic) { XDocument _XDoc = XDocument.Load(uri); if (_XDoc == null) { _XDoc.Save(uri); } XElement _Root = _XDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } _Root.RemoveAll(); foreach (string _id in _dic.Keys) { _Root.Add(new XElement(_id, StringEx.ConverToString(_dic[_id]))); } _XDoc.Save(uri); }
//加上相应的域信息 private static void AddFieldInfos(XmlDocument xmlDoc, XmlElement fieldEle, Component prefabComp, Component objComp) { Type compType = prefabComp.GetType(); XmlElement compEle = xmlDoc.CreateElement(compType.Name); FieldInfo[] fieldInfos = compType.GetFields(); for (int infoNum = 0; infoNum < fieldInfos.Length; infoNum++) { FieldInfo fieldInfo = fieldInfos[infoNum]; if (StringEx.IsConvertableType(fieldInfo.FieldType) && fieldInfo.IsPublic == true)//为公共类型并且与Prefab不一样了 { object prefabValue = fieldInfo.GetValue(prefabComp); object objValue = fieldInfo.GetValue(objComp); if (!prefabValue.Equals(objValue)) { Debug.Log(compType.Name); Debug.Log(fieldInfos[infoNum].Name); Debug.Log(prefabValue + " " + objValue); compEle.SetAttribute(fieldInfo.Name, StringEx.ConverToString(objValue)); } } } if (compEle.HasAttributes) { fieldEle.AppendChild(compEle); } }
/// <summary> /// 加入词典 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xDoc"></param> /// <param name="nodeNames"></param> /// <param name="_dic"></param> /// <returns></returns> public static XDocument WriteDictionary <T>(this XDocument xDoc, string[] nodeNames, Dictionary <string, T> _dic) { XElement _Root = xDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } _Root.RemoveAll(); foreach (string _id in _dic.Keys) { _Root.Add(new XElement(_id, StringEx.ConverToString(_dic[_id]))); } return(xDoc); }
/// <summary> /// 加入列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="uri"></param> /// <param name="nodeNames"></param> /// <param name="arr"></param> /// <param name="tag"></param> public static XDocument WriteList <T>(this XDocument xDoc, string[] nodeNames, List <T> arr, string tag = "item") { XElement _Root = xDoc.Root; for (int i = 0; i < nodeNames.Length; i++) { if (_Root.Element(nodeNames[i]) == null) { _Root.Add(new XElement(nodeNames[i])); } _Root = _Root.Element(nodeNames[i]); } _Root.RemoveAll(); foreach (T ob in arr) { _Root.Add(new XElement(tag, StringEx.ConverToString(ob))); } return(xDoc); }
private static void AddPropInfos(XmlDocument xmlDoc, XmlElement propEle, Component prefabComp, Component objComp) { Type compType = prefabComp.GetType(); XmlElement compEle = xmlDoc.CreateElement(compType.Name); PropertyInfo[] propInfos = compType.GetProperties(); for (int infoNum = 0; infoNum < propInfos.Length; infoNum++) { PropertyInfo propInfo = propInfos[infoNum]; if (StringEx.IsConvertableType(propInfo.PropertyType) && propInfo.GetGetMethod() != null && propInfo.GetGetMethod().IsPublic == true && propInfo.GetSetMethod() != null && propInfo.GetSetMethod().IsPublic == true) { object prefabValue = propInfo.GetValue(prefabComp, null); object objValue = propInfo.GetValue(objComp, null); if (prefabValue == null || objValue == null) { continue; } if (!prefabValue.Equals(objValue)) { Debug.Log(compType.Name); Debug.Log(propInfos[infoNum].Name); Debug.Log(prefabValue + "!!!!!!!!!!!!!!!!!" + objValue); compEle.SetAttribute(propInfo.Name, StringEx.ConverToString(objValue)); } } } if (compEle.HasAttributes) { propEle.AppendChild(compEle); } }