public static XmlName GetXmlName(this IXmlObject xmlObject) { Type xmlObjectType; XmlName xmlName; XmlElementMappingAttribute xmlElementMappingAttribute; if ((object)xmlObject == null) { throw new ArgumentNullException(nameof(xmlObject)); } xmlObjectType = xmlObject.GetType(); xmlElementMappingAttribute = SolderFascadeAccessor.ReflectionFascade.GetOneAttribute <XmlElementMappingAttribute>(xmlObjectType); if ((object)xmlElementMappingAttribute == null) { xmlName = null; } else { xmlName = new XmlName() { LocalName = xmlElementMappingAttribute.LocalName, NamespaceUri = xmlElementMappingAttribute.NamespaceUri }; } return(xmlName); }
public override void Configure(IXmlObject obj, XElement source) { var array = obj as IXmlArray; if (array == null) return; // Clear array.ClearItems(); var itemElems = source.Elements("item").ToList(); if (itemElems.Any()) { foreach (var elem in itemElems) { if (elem == null) continue; try { // Append items var node = XmlInstantiator.InstantiateNode(elem, array); array.AddItem(node); } catch (Exception ex) { throw; } } } }
public override void Configure(IXmlObject obj, XElement source) { var propertyElems = source.Elements("property").Where(x => x != null).ToList(); if (propertyElems.Any()) { foreach (var elem in propertyElems) { var propertyName = elem.GetAttributeValue("name"); if (string.IsNullOrEmpty(propertyName)) continue; try { var property = MirrorProperty.Create(obj, propertyName); object value = XmlInstantiator.InstantiateFromElement(elem, obj); value = XmlSettings.Converter.Convert(value, property.Type); property.SetValue(value); //var propertyInfo = target.GetType().GetProperty(propertyName); //if (propertyInfo != null) //{ // value = Converter.Convert(value, propertyInfo.PropertyType); // propertyInfo.SetValue(target, value, null); //} //else // throw new Exception($"Property not found {propertyName}"); } catch (Exception ex) { throw; } } } }
//============================================================ // <T>加载对象节点设置。</T> // // @param parent 父对象 // @param nodes 节点列表 //============================================================ protected virtual void LoadNodes(IXmlObject parent, FXmlNodes nodes) { if ((null != nodes) && !nodes.IsEmpty()) { foreach (FXmlNode node in nodes) { FXmlObjectConfig config = _configs[node.Name]; if (null != config) { // 创建实例 IXmlObject instance = config.CreateInstance <IXmlObject>(); _context._parent = parent; _context._config = node; instance.LoadConfig(_context); if (null != parent) { parent.Push(instance); } // 加载子节点 if (node.HasNode()) { LoadNodes(instance, node.Nodes); } } } } }
public override void Export(IXmlObject obj, XElement target) { var array = obj as IXmlArray; if (array == null) return; // Clear target.Elements("item").Remove(); var nodes = array.Items().ToList(); if (nodes.Any()) { foreach (var subNode in nodes) { if (subNode == null) continue; var type = subNode.GetType(); var typeString = type.FullName + ", " + type.Assembly.GetName().Name; // Append items var elem = new XElement("item"); target.Add(elem); elem.SetAttributeValue("type", typeString); //node.Export(elem); var subObj = subNode as XmlObject; if (subObj != null) { var pattern = (obj as XmlObject)?.Pattern ?? subObj.Pattern; pattern.Export(subObj, elem); } } } }
public override IXmlObject DeserializeXmlObject(string value) { IXmlObject ret = null; ret = (IXmlObject)SerializationHelper.Deserialize(typeof(NewReleaseMessage), value); return(ret); }
public void WriteXmlObjectToFile(IXmlObject xmlObject, string fileName) { IXmlGenerator gen = Factory.GetGenerator(); var str = gen.SerializeXmlObject(xmlObject); gen.WriteXmlFile(fileName, str); System.IO.File.WriteAllText(fileName, str); }
public virtual void Configure(IXmlObject obj, XElement source) { foreach (var convention in Conventions) { convention.Configure(obj, source); } //obj.Configure(target); }
/// <summary> /// Initializes a new instance of the XmlObjectCollection class. /// </summary> /// <param name="site"> The containing site XML object. </param> public XmlObjectCollection(IXmlObject site) { if ((object)site == null) { throw new ArgumentNullException(nameof(site)); } this.site = site; }
public virtual void Export(IXmlObject obj, XElement target) { foreach (var convention in Conventions) { convention.Export(obj, target); } //obj.Export(target); }
public override void Export(IXmlObject obj, XElement target) { var properties = obj.GetProperties(); foreach (var property in properties) { if (property == null) continue; GetOrUpdateProperty(target, property); } }
//============================================================ // <T>根据对象查找设置。</T> // // @param xobject 对象 // @return 类型设置 //============================================================ public FXmlObjectConfig FindByType(IXmlObject xobject) { string typeName = xobject.GetType().FullName; FXmlObjectConfig config = _types[typeName]; if (null == config) { throw new FFatalException("Undefine type({0}).", typeName); } return(config); }
public override void Configure(IXmlObject obj, XElement source) { var propertyElems = source.Elements("property").Where(x => x != null).ToList(); if (propertyElems.Any()) { foreach (var elem in propertyElems) { var propertyName = elem.GetAttributeValue("name"); if (string.IsNullOrEmpty(propertyName)) continue; try { object value = XmlInstantiator.InstantiateFromElement(elem, obj); ////if (target is IHasProperties) ////{ //// var hasProps = (IHasProperties) target; //// hasProps.Properties[propertyName] = value; ////} ////else //{ // var propertyInfo = target.GetType().GetProperty(propertyName); // if (propertyInfo != null) // { // value = Converter.Convert(value, propertyInfo.PropertyType); // propertyInfo.SetValue(target, value, null); // } // else // throw new Exception($"Property not found {propertyName}"); //} var property = obj.GetProperty(propertyName); if (property != null) { if (property.Type != null) { if (ConvertValues) value = Converter.Convert(value, property.Type); } property.SetValue(value); } else { //throw new Exception($"Property '{propertyName}' not found"); obj.SetPropertyValue(propertyName, value); } } catch (Exception ex) { throw; } } } }
static void WriteXmlObject(XmlWriter writer, IXmlObject xmlObject) { foreach (var proprety in xmlObject.GetType().GetProperties()) { if (proprety.IncludeInVXml()) { writer.WriteStartElement(proprety.GetXmlAlias()); writer.WriteString(proprety.GetValue(xmlObject)?.ToString() ?? ""); writer.WriteEndElement(); } } }
//============================================================ // <T>为一个文档对象加载文件。</T> // // @param top 文档对象 // @param fileName 文件名称 //============================================================ public void LoadFile(IXmlObject top, string filename) { if (_logger.DebugAble) { _logger.Debug(this, "LoadFile", "Load {0}={1}", top, filename); } FXmlDocument document = new FXmlDocument(); _context._top = top; document.LoadFile(filename); LoadNodes(top, document.Root.Nodes); }
public override void Export(IXmlObject obj, XElement target) { var mirror = new MirrorObjectModel(obj); //var properties = target.GetProperties(); var properties = mirror.GetProperties(); foreach (var property in properties) { if (property == null) continue; GetOrUpdateProperty(target, property); } }
//============================================================ // <T>保存对象为文件。</T> // // @param top 顶层对象 // @param fileName 文件名称 //============================================================ public void SaveFile(IXmlObject top, string fileName) { if (_logger.DebugAble) { _logger.Debug(this, "SaveFile", "Save {0}={1}", top, fileName); } _context._top = top; // 保存文件 FXmlDocument document = new FXmlDocument(); document.Root.Name = "Configuration"; SaveNodes(top, document.Root); document.SaveFile(fileName); }
/// <summary> /// Ensures that for any XML object property, the correct parent instance is set/unset. /// Should be called in the setter for all XML object properties, before assigning the value. /// Example: /// set { this.EnsureParentOnPropertySet(this.content, value); this.content = value; } /// </summary> /// <param name="oldValueObj"> The old XML object value (the backing field). </param> /// <param name="newValueObj"> The new XML object value (value). </param> protected void EnsureParentOnPropertySet(IXmlObject oldValueObj, IXmlObject newValueObj) { if ((object)oldValueObj != null) { oldValueObj.Surround = null; oldValueObj.Parent = null; } if ((object)newValueObj != null) { newValueObj.Surround = null; newValueObj.Parent = this; } }
//============================================================ // <T>保存对象为节点。</T> // // @param parent 父对象 // @param node 设置节点 //============================================================ protected virtual void SaveNodes(IXmlObject parent, FXmlNode node) { /*if (null != parent.Children) { * foreach (IXmlObject child in parent.Children) { * FXmlObjectConfig config = _configs.FindByType(child); * FXmlNode childNode = node.CreateNode(config.Name); * // Instance * _context._parent = parent; * _context._config = childNode; * child.SaveConfig(_context); * // Children * if (child.Children != null) { * SaveNodes(child, childNode); * } * } * }*/ }
public void CompareXml(string expected, IXmlObject apiFunction) { Stream stream = new MemoryStream(); XmlWriterSettings xmlSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, IndentChars = " " }; IaXmlWriter xml = new IaXmlWriter(stream, xmlSettings); apiFunction.WriteXml(ref xml); xml.Flush(); stream.Position = 0; StreamReader reader = new StreamReader(stream); Assert.Equal(expected, reader.ReadToEnd()); }
public abstract void Export(IXmlObject obj, XElement target);
public IBindableModel GetModelFromXmlObject(IXmlObject xmlObject) { AudioAlbumModel ret = new AudioAlbumModel(); ret.FullFileName = xmlObject.FullFileName; NewReleaseMessage nrm = (NewReleaseMessage)xmlObject; if (nrm != null) { if (nrm.MessageHeader != null) { ret.MessageID = nrm.MessageHeader.MessageId; if (nrm.MessageHeader.MessageSender.PartyId.Length > 0) { ret.SenderPartyID = nrm.MessageHeader.MessageSender.PartyId[0].Value; } if (nrm.MessageHeader.MessageSender.PartyName != null && nrm.MessageHeader.MessageSender.PartyName.FullName != null) { ret.SenderPartyName = nrm.MessageHeader.MessageSender.PartyName.FullName.Value; } if (nrm.MessageHeader.MessageRecipient != null && nrm.MessageHeader.MessageRecipient.Length > 0 && nrm.MessageHeader.MessageRecipient[0].PartyId != null && nrm.MessageHeader.MessageRecipient[0].PartyId.Length > 0) { ret.RecipientPartyID = nrm.MessageHeader.MessageRecipient[0].PartyId[0].Value; } if (nrm.MessageHeader.MessageRecipient != null && nrm.MessageHeader.MessageRecipient.Length > 0 && nrm.MessageHeader.MessageRecipient[0].PartyName != null && nrm.MessageHeader.MessageRecipient[0].PartyName.FullName != null) { ret.RecipientPartyName = nrm.MessageHeader.MessageRecipient[0].PartyName.FullName.Value; } ret.MessageCreatedDateTime = nrm.MessageHeader.MessageCreatedDateTime; ret.UpdateIndicator = nrm.UpdateIndicator; if (nrm.ReleaseList != null && nrm.ReleaseList.Release != null && nrm.ReleaseList.Release.Length > 0 && nrm.ReleaseList.Release[0].ReleaseId != null && nrm.ReleaseList.Release[0].ReleaseId.Length > 0 && nrm.ReleaseList.Release[0].ReleaseId[0].ICPN != null) { ret.EAN = nrm.ReleaseList.Release[0].ReleaseId[0].ICPN.Value; } if (nrm.ReleaseList != null && nrm.ReleaseList.Release != null && nrm.ReleaseList.Release.Any()) { ret.Tracks.RaiseListChangedEvents = false; foreach (var rel in nrm.ReleaseList.Release.Where(x => !x.IsMainRelease)) { var track = new TrackModel() { Ordinal = Convert.ToInt32(rel.ReleaseReference.FirstOrDefault().TrimStart('R')), ISRC = rel.ReleaseId.FirstOrDefault().ISRC, Title = rel.ReferenceTitle.TitleText.Value }; if (rel.ReleaseDetailsByTerritory != null && rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre != null && rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre.FirstOrDefault().GenreText != null) { track.Genre = rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre.FirstOrDefault().GenreText.Value; } if (rel.ReleaseDetailsByTerritory != null && rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre != null && rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre.FirstOrDefault().SubGenre != null) { track.SubGenre = rel.ReleaseDetailsByTerritory.FirstOrDefault().Genre.FirstOrDefault().SubGenre.Value; } ret.Tracks.Add(track); } ret.Tracks.RaiseListChangedEvents = true; ret.Tracks.ResetBindings(); } } } return(ret); }
/// <summary> /// Ensures that for any XML object property, the correct parent instance is set/unset. /// Should be called in the setter for all XML object properties, before assigning the value. /// Example: /// set { this.EnsureParentOnPropertySet(this.content, value); this.content = value; } /// </summary> /// <param name="oldValueObj"> The old XML object value (the backing field). </param> /// <param name="newValueObj"> The new XML object value (value). </param> protected void EnsureParentOnPropertySet(IXmlObject oldValueObj, IXmlObject newValueObj) { if ((object)oldValueObj != null) oldValueObj.Parent = null; if ((object)newValueObj != null) newValueObj.Parent = this; }
public abstract void Configure(IXmlObject obj, XElement source);
public string SerializeXmlObject(IXmlObject value) { string xml = Business.DDEXFactory.Helpers.SerializationHelper.Serialize(value); return(xml); }