private static XElement BuildElement(IReflectionProperty property, string elementName = null) { XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute); XNamespace defaultNamspace = null; List <XAttribute> namespaceList = new List <XAttribute>(); var nodeName = property.Name; if (attribute != null) { defaultNamspace = attribute.DefaultNamespace; namespaceList.AddRange(attribute.NamespaceList.Select(x => new XAttribute(XNamespace.Xmlns + x.Key, x.Value)).ToList()); if (!string.IsNullOrWhiteSpace(attribute.Name)) { nodeName = attribute.Name; } } if (!string.IsNullOrWhiteSpace(elementName)) { nodeName = elementName; } XElement xElement = defaultNamspace != null ? new XElement(defaultNamspace + nodeName, namespaceList) : new XElement(nodeName, namespaceList); return(xElement); }
private static XObject BuildPrimitiveProperty(IReflectionProperty property, object value) { XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute); XNamespace defaultNamspace = null; List <XAttribute> namespaceList = new List <XAttribute>(); var nodeName = property.Name; if (property.IsEnumerable && !property.IsDictionary && property.EnumerableType != null) { nodeName = property.EnumerableType.Name; } if (attribute != null) { defaultNamspace = attribute.DefaultNamespace; namespaceList.AddRange(attribute.NamespaceList.Select(x => new XAttribute(XNamespace.Xmlns + x.Key, x.Value)).ToList()); if (!string.IsNullOrWhiteSpace(attribute.Name)) { nodeName = attribute.Name; } if (property.IsEnumerable && !property.IsDictionary && property.EnumerableType != null && !string.IsNullOrWhiteSpace(attribute.ItemName)) { nodeName = attribute.ItemName; } } XElement xElement = defaultNamspace != null ? new XElement(defaultNamspace + nodeName, namespaceList) : new XElement(nodeName, namespaceList); xElement.SetValue(FormatValue(property, value)); return(xElement); }
public void TestClassInheritanceSetsAll() { var obj = new XmlClassInherit(); _doc.LoadXml("<body><int>3</int><subclassInt>42</subclassInt></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, obj); Assert.AreEqual(3, obj.Int); Assert.AreEqual(42, obj.SubClassInt); }
private void SetProperty(object instance, XElement root, IReflectionProperty property) { if (property.IsPrimitive || property.IsNullable) { if (instance != null) { SetPrimitiveProperty(property, instance, root); } } if (property.IsClass) { var type = Reflector.Get(property.Type); object value = null; if (property.CanRead) { value = property.Get(instance); } if (value == null) { if (property.CanWrite) { value = type.CreateInstance(); } } if (value != null) { var properties = type.Properties.OrderBy( p => { OrderAttribute priority = p.Attributes.SingleOrDefault(x => x is OrderAttribute) as OrderAttribute; return(priority != null ? priority.Value : int.MaxValue); }).ToList(); XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute); var propertyName = property.Name; if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Name)) { propertyName = attribute.Name; } var element = root.Element(propertyName); if (element != null) { this.SetPropertiesValue(properties, value, element); } } } }
/// <inheritdoc/> /// <summary> /// Returns direct URL to file if it is a File item, otherwise /// builds url from items /// </summary> public override Item DoApply(Item root) { // populate properties from xml XmlPropertyAttribute.BindXml(root.node, this); // log info Log(nameof(DoApply), $"Generating URL for {Type} ID {Id})"); // generate url var url = Type == "File" ? Innovator.getFileUrl(Id, UrlType.None) : $"{BaseUrl}/default.aspx?StartItem={Type}:{Id}"; // return result return(Innovator.newResult(url)); }
private void SetPrimitiveProperty(IReflectionProperty property, object instance, XElement root) { XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute); var propertyName = property.Name; if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Name)) { propertyName = attribute.Name; } var element = root.Element(propertyName); if (element != null) { var value = element.Value; if (!string.IsNullOrWhiteSpace(value)) { property.Set(instance, value); } } }
private static XObject BuildDictionaryProperty(IReflectionProperty property, object value) { XElement root = BuildElement(property); if (value != null) { if (property.KeyType.IsPrimitiveType() && property.EnumerableType.IsPrimitiveType()) { IDictionary items = (IDictionary)value; foreach (DictionaryEntry item in items) { XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute); XElement child = BuildElement(property, attribute != null ? attribute.ItemName : null); //child.Add(Bu(property, attribute != null ? attribute.ItemName : null);); root.Add(child); } } } return(root); }
public void TestRequiredSet() { _doc.LoadXml("<body><int>3</int></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _objReq); Assert.AreEqual(3, _objReq.Int); }
public void TestDefaultName() { _doc.LoadXml("<body><int>47</int></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _obj); Assert.AreEqual(47, _obj.Int); }
public void TestConvert() { _doc.LoadXml("<body><thebool>true</thebool></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _obj); Assert.AreEqual(true, _obj.Bool); }
public void TestBindSimple() { _doc.LoadXml("<body><thestring>42</thestring></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _obj); Assert.AreEqual("42", _obj.String); }
public void TestIgnoresText() { _doc.LoadXml("<body> bah </body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _obj); Assert.AreEqual(null, _obj.String); }
public void TestBindNothing() { _doc.LoadXml("<body></body>"); XmlPropertyAttribute.BindXml(_doc.DocumentElement, _obj); Assert.AreEqual(null, _obj.String); }
public void TestUnknownElementNameThrows() { _doc.LoadXml("<body><fail>indeed</fail></body>"); ExceptionAssert.Throws <XmlException>(() => XmlPropertyAttribute.BindXml(_doc.DocumentElement, _objReq)); }
public void TestMissingRequiredThrows() { _doc.LoadXml("<body></body>"); ExceptionAssert.Throws <XmlException>(() => XmlPropertyAttribute.BindXml(_doc.DocumentElement, _objReq)); }