private static void WriteElements(XmlWriter writer, XmlNamespaceManager manager, Element element) { var browser = TypeBrowser.Create(element.GetType()); foreach (Tuple <PropertyInfo, KmlElementAttribute> property in browser.Elements) { object value = property.Item1.GetValue(element, null); // Make sure it needs saving if (value != null) { // Is this an element? if (property.Item2.ElementName == null) { SerializeElement(writer, manager, (Element)value); } else { writer.WriteStartElement(property.Item2.ElementName, property.Item2.Namespace); WriteData(writer, GetString(value)); writer.WriteEndElement(); } } } }
private static void WriteElements(XmlWriter writer, XmlNamespaceManager manager, Element element) { var browser = TypeBrowser.Create(element.GetType()); foreach (TypeBrowser.ElementInfo elementInfo in browser.Elements) { object value = elementInfo.GetValue(element); // Make sure it needs saving if (value != null) { // Is this an element? if (string.IsNullOrEmpty(elementInfo.Component.Name)) { SerializeElement(writer, manager, (Element)value); } else { writer.WriteStartElement(elementInfo.Component.Name, elementInfo.Component.NamespaceUri); WriteData(writer, GetString(value)); writer.WriteEndElement(); } } } }
private void AddChild(Element parent) { Element child = this.CreateElement(); bool isOrphan; if (child is UnknownElement) { var browser = TypeBrowser.Create(parent.GetType()); PropertyInfo property = browser.FindElement(this.GetXmlComponent()); if (property != null) { // We're not going to add it to the parent, which has the potential to // lose any attributes/child elements assigned to the unknown, but this // is the behaviour of the C++ version. this.PopulateElement(child); AssignValue(parent, property, child.InnerText); return; } isOrphan = true; } else { isOrphan = !this.AddChildToParent(parent, child); } this.PopulateElement(child); this.OnElementAdded(child); if (isOrphan) { parent.AddOrphan(child); // Save for later serialization } }
private void ProcessAttributes(Element element) { var browser = TypeBrowser.Create(element.GetType()); while (this.reader.MoveToNextAttribute()) { // Check for namespaces first if (string.Equals("xmlns", this.reader.Name, StringComparison.Ordinal)) { // Set default namespace only on unknown elements if (element is UnknownElement) { element.AddNamespace(string.Empty, this.reader.Value); } } else if (string.Equals("xmlns", this.reader.Prefix, StringComparison.Ordinal)) { element.AddNamespace(this.reader.LocalName, this.reader.Value); } else { // just a normal attribute PropertyInfo property = browser.FindAttribute(new XmlComponent(null, this.reader.LocalName, null)); // Attributes never have namespace info. if (property != null) { AssignValue(element, property, this.reader.Value); } else { // Unknown, save for later serialization element.AddAttribute(new XmlComponent(this.reader)); } } } }
public ElementSerializer(Serializer serializer, XmlWriter writer, XmlNamespaceManager manager, Type elementType) { this.manager = manager; this.serializedElements = new HashSet <Element>(); this.serializer = serializer; this.typeBrowser = TypeBrowser.Create(elementType); this.writer = writer; }
private static void RegisterElement(Type type) { KmlElementAttribute element = TypeBrowser.GetElement(type.GetTypeInfo()); if (element != null) { var xml = new XmlComponent(null, element.ElementName, element.Namespace); RegisterType(xml, type); } }
private void WriteAttributes(Element element) { TypeBrowser browser = TypeBrowser.Create(element.GetType()); foreach (var property in browser.Attributes) { object value = property.Item1.GetValue(element, null); if (value != null) // Make sure it needs saving { _writer.WriteAttributeString(property.Item2.AttributeName, GetString(value)); } } }
/// <summary> /// Creates TypeBrowser representing the specified type. /// </summary> /// <param name="type">The type to extract properties from.</param> /// <returns> /// A TypeBrowser containing information about the specified type. /// </returns> public static TypeBrowser Create(Type type) { TypeBrowser browser; lock (_typesLock) { if (!_types.TryGetValue(type, out browser)) { browser = new TypeBrowser(type); _types.Add(type, browser); } } return(browser); }
private static string GetString(object value) { Type type = value.GetType(); if (type.IsEnum) { KmlElementAttribute att = TypeBrowser.GetEnum((Enum)value); if (att != null) { return(att.ElementName); } } return(string.Format(KmlFormatter.Instance, "{0}", value)); }
private static void RegisterAssembly(Assembly assembly) { foreach (var type in assembly.GetExportedTypes()) { if (type.IsSubclassOf(typeof(Element))) { KmlElementAttribute element = TypeBrowser.GetElement(type); if (element != null) { var xml = new XmlComponent(null, element.ElementName, element.Namespace); RegisterType(xml, type); } } } }
private static Dictionary <string, object> GetEnumValues(TypeInfo enumType) { var lookup = new Dictionary <string, object>(StringComparer.Ordinal); foreach (FieldInfo field in enumType.DeclaredFields.Where(f => f.IsStatic)) { KmlElementAttribute element = TypeBrowser.GetElement(field); if (element != null) { lookup.Add(element.ElementName, field.GetValue(null)); } } return(lookup); }
private static void WriteAttributesForElement(XmlWriter writer, Element element) { var browser = TypeBrowser.Create(element.GetType()); foreach (TypeBrowser.ElementInfo attribute in browser.Attributes) { object value = attribute.GetValue(element); // Make sure it needs saving if (value != null) { writer.WriteAttributeString(attribute.Component.Name, GetString(value)); } } }
private static void WriteAttributesForElement(XmlWriter writer, Element element) { var browser = TypeBrowser.Create(element.GetType()); foreach (Tuple <PropertyInfo, KmlAttributeAttribute> property in browser.Attributes) { object value = property.Item1.GetValue(element, null); // Make sure it needs saving if (value != null) { writer.WriteAttributeString(property.Item2.AttributeName, GetString(value)); } } }
private static bool AssignToProperty(Element parent, Element child) { TypeInfo childType = child.GetType().GetTypeInfo(); var browser = TypeBrowser.Create(parent.GetType()); foreach (TypeBrowser.ElementInfo elementInfo in browser.Elements) { if (elementInfo.ValueType.GetTypeInfo().IsAssignableFrom(childType)) { elementInfo.SetValue(parent, child); return(true); } } return(false); }
private static object GetEnum(Type type, string value) { if (value != null) { value = value.Trim(); foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { KmlElementAttribute element = TypeBrowser.GetElement(field); if (element != null && string.Equals(element.ElementName, value, StringComparison.Ordinal)) { return(field.GetValue(null)); } } } return(null); }
private static object GetEnum(TypeInfo typeInfo, string value) { if (value != null) { value = value.Trim(); foreach (FieldInfo field in typeInfo.DeclaredFields.Where(f => f.IsStatic)) { KmlElementAttribute element = TypeBrowser.GetElement(field); if (element != null && string.Equals(element.ElementName, value, StringComparison.Ordinal)) { return(field.GetValue(null)); } } } return(null); }
private void AddChild(Element parent) { TypeBrowser browser = TypeBrowser.Create(parent.GetType()); Element child = this.GetElement(); if (child is UnknownElement) { PropertyInfo property = browser.FindElement(this.GetXmlComponent()); if (property != null) { AssignValue(parent, property, child.InnerText); // We're not going to add it to the parent, which has the potential to // lose any attributes/child elements assigned to the unknown, but this // is the behaviour of the C++ version. return; } } else if (parent.AddChild(child)) { this.OnElementAdded(child); // Call it here after it's Parent has been set return; // Not an orphan } else { // Lets try an Element as a property? this.OnElementAdded(child); // Will be either added as a Property or Orphan // Search for a property that we can assign to TypeInfo typeInfo = child.GetType().GetTypeInfo(); foreach (var property in browser.Elements) { if (property.Item1.PropertyType.GetTypeInfo().IsAssignableFrom(typeInfo)) { property.Item1.SetValue(parent, child, null); return; } } } parent.AddOrphan(child); // Save for later serialization }
// Because the Element's won't be nested too deep (max 100), a Stack based // iterative approach is not necessary so recursion is used for clarity. private static IEnumerable <Element> WalkElement(Element element) { var customElement = element as ICustomElement; if ((customElement == null) || customElement.ProcessChildren) { yield return(element); // Is a valid Element // Explore the properties var browser = TypeBrowser.Create(element.GetType()); foreach (TypeBrowser.ElementInfo info in browser.Elements) { foreach (Element property in GetPropertyElements(element, info)) { foreach (Element e in WalkElement(property)) { yield return(e); } } } } }
private void WriteElements(Element element) { TypeBrowser browser = TypeBrowser.Create(element.GetType()); foreach (var property in browser.Elements) { object value = property.Item1.GetValue(element, null); if (value != null) // Make sure it needs saving { if (property.Item2.ElementName == null) // This is an element { this.SerializeElement((Element)value); } else { _writer.WriteStartElement(property.Item2.ElementName, property.Item2.Namespace); this.WriteData(GetString(value)); _writer.WriteEndElement(); } } } }
private bool AddChildToParent(Element parent, Element child) { if (parent.AddChild(child)) { return(true); } else { // Search for a property that we can assign to TypeInfo typeInfo = child.GetType().GetTypeInfo(); var browser = TypeBrowser.Create(parent.GetType()); foreach (PropertyInfo property in browser.Elements.Select(x => x.Item1)) { if (property.PropertyType.GetTypeInfo().IsAssignableFrom(typeInfo)) { property.SetValue(parent, child, null); return(true); } } } return(false); }
private string GetString(object value) { TypeInfo typeInfo = value.GetType().GetTypeInfo(); if (typeInfo.IsEnum) { KmlElementAttribute att = TypeBrowser.GetEnum((Enum)value); if (att != null) { return(att.ElementName); } } if (((this.Options & SerializerOptions.ReadableFloatingPoints) != 0) && (value is double || value is float)) { return(string.Format(KmlFormatter.Instance, "{0:G}", value)); } else { return(string.Format(KmlFormatter.Instance, "{0}", value)); } }
private bool AddChildToParent(Element parent, Element child) { if (parent.TryAddChild(child)) { return(true); } else { // Search for a property that we can assign to TypeInfo typeInfo = child.GetType().GetTypeInfo(); var browser = TypeBrowser.Create(parent.GetType()); foreach (TypeBrowser.ElementInfo elementInfo in browser.Elements) { if (elementInfo.ValueType.GetTypeInfo().IsAssignableFrom(typeInfo)) { elementInfo.SetValue(parent, child); return(true); } } } return(false); }
// Because the Element's won't be nested too deep (max 100), a Stack based // iterative approach is not necessary so recursion is used for clarity. private static IEnumerable <Element> WalkElement(Element element) { var customElement = element as ICustomElement; if ((customElement == null) || customElement.ProcessChildren) { yield return(element); // Is a valid Element // Explore the children foreach (Element child in element.Children) { foreach (Element e in WalkElement(child)) { yield return(e); } } // Explore the properties var browser = TypeBrowser.Create(element.GetType()); foreach (TypeBrowser.ElementInfo info in browser.Elements) { // All properties with their ElementName set to null will be Elements // Check here to avoid the GetValue the property is not an Element. if (string.IsNullOrEmpty(info.Component.Name)) { object value = info.GetValue(element); if (value != null) { foreach (Element e in WalkElement((Element)value)) { yield return(e); } } } } } }
// Because the Element's won't be nested too deep (max 100), a Stack based // iterative approach is not necessary so recursion is used for clarity. private static IEnumerable <Element> WalkElement(Element element) { ICustomElement customElement = element as ICustomElement; if ((customElement == null) || customElement.ProcessChildren) { yield return(element); // Is a valid Element // Explore the children foreach (var child in element.Children) { foreach (var e in WalkElement(child)) { yield return(e); } } // Explore the properties TypeBrowser browser = TypeBrowser.Create(element.GetType()); foreach (var property in browser.Elements) { // All properties with their ElementName set to null will be Elements // Check here to avoid the GetValue the property is not an Element. if (property.Item2.ElementName == null) { object value = property.Item1.GetValue(element, null); if (value != null) { foreach (var e in WalkElement((Element)value)) { yield return(e); } } } } } }