public static void SaveToFile(this IXml xml, string fileName) { XElement ele = new XElement("Root"); xml.Save(ele); ele.Save(fileName); }
public static void Save(IXml node, Stream stream) { XmlDocument doc = new XmlDocument(); doc.AppendChild(node.ToXml(doc)); doc.Save(stream); }
public static void Load(IXml node, Stream stream) { XmlDocument doc = new XmlDocument(); doc.Load(stream); node.FromXml(doc.DocumentElement); }
public static void Load(IXml node, string fileName) { XmlDocument doc = new XmlDocument(); doc.Load(fileName); node.FromXml(doc.DocumentElement); }
public void Add(IXml node) { if (node != null) { SubList.Add(node); } }
public XmlItem(Symbol name, IXml xmlItem, params IXml[] xmlItems) : this(name) { Add(xmlItem); foreach (var item in xmlItems) { Add(item); } }
public static void Save(IXml node, string fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } using (Stream stream = File.Create(fileName)) { Save(node, stream); } }
public XmlElement ToXml(XmlDocument document) { XmlElement result = document.CreateElement("node"); result.SetAttribute("class", GetType().AssemblyQualifiedName); result.SetAttribute("name", Name); foreach (IDocLeaf item in Children()) { IXml itemXml = item as IXml; result.AppendChild(itemXml.ToXml(document)); } return(result); }
public static void SaveToXml(object source, XElement target) { Type type = source.GetType(); List <MemberInfo> members = GetMemberInfos(type); foreach (var item in members) { object[] attributes = item.GetCustomAttributes(typeof(RcpaOptionAttribute), false); foreach (RcpaOptionAttribute fieldAttribute in attributes) { //获得相应属性的值 object value = type.InvokeMember(item.Name, BindingFlags.GetProperty | BindingFlags.GetField, null, source, null); target.RemoveChild(fieldAttribute.Name); if (fieldAttribute.ValueType == RcpaOptionType.StringArray) { string[] values = (string[])value; target.Add(new XElement(fieldAttribute.Name, from v in values select new XElement("item", v))); } else if (fieldAttribute.ValueType == RcpaOptionType.StringList) { List <string> values = (List <string>)value; target.Add(new XElement(fieldAttribute.Name, from v in values select new XElement("item", v))); } else if (fieldAttribute.ValueType == RcpaOptionType.IXml) { IXml obj = value as IXml; var parent = new XElement(fieldAttribute.Name); obj.Save(parent); target.Add(parent); } else { target.Add(new XElement(fieldAttribute.Name, value)); } } } }
public static void LoadFromXml(object target, XElement source) { Type type = target.GetType(); List <MemberInfo> members = GetMemberInfos(type); foreach (var item in members) { object[] attributes = item.GetCustomAttributes(typeof(RcpaOptionAttribute), true); foreach (RcpaOptionAttribute fieldAttribute in attributes) { var xmlElement = source.Element(fieldAttribute.Name); if (xmlElement != null) { object oldValue = type.InvokeMember(item.Name, BindingFlags.GetProperty | BindingFlags.GetField, null, target, null); switch (fieldAttribute.ValueType) { case RcpaOptionType.String: { var xmlValue = xmlElement.Value; type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.Int32: { var xmlValue = Convert.ToInt32(xmlElement.Value); type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.Double: { var xmlValue = MyConvert.ToDouble(xmlElement.Value); type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.Boolean: { var xmlValue = Convert.ToBoolean(xmlElement.Value); type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.StringArray: { var xmlValue = (from n in xmlElement.Elements() select n.Value).ToArray(); type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.StringList: { var xmlValue = (from n in xmlElement.Elements() select n.Value).ToList(); type.InvokeMember(item.Name, BindingFlags.SetProperty | BindingFlags.SetField, null, target, new object[] { xmlValue }); break; } case RcpaOptionType.IXml: { IXml xmlValue = oldValue as IXml; xmlValue.Load(xmlElement); break; } default: throw new ArgumentException(MyConvert.Format("Unsupported RcpaOptionType {0} of field/property {1} in type {2}, only String/Int32/Double supported", fieldAttribute.ValueType, item.Name, type.Name)); } } } } }
public static void LoadFromFile(this IXml xml, string fileName) { XElement ele = XElement.Load(fileName); xml.Load(ele); }
public static bool Deserializar(IXml xml, out object obj) { return(xml.Leer(@"C:\Users\sopelan1\Desktop\programacion_laboratorio_2\Sopelana.Marcos\Lista de personas.xml", out obj)); }
public static bool Serializar(IXml xml) { return(xml.Guardar(@"C:\Users\sopelan1\Desktop\programacion_laboratorio_2\Sopelana.Marcos\Lista de personas.xml")); }