/// <summary> /// Creates an XElement from the given IRecord /// </summary> /// <param name="obj"></param> /// <returns></returns> private XElement GetBeerXElement(IRecord obj) { XElement element = new XElement(obj.GetType().Name.ToUpperInvariant()); Type objType = obj.GetType(); foreach (KeyValuePair <string, BeerXMLProperty> typeProperty in BeerXMLProperty.GetBeerXMLPropertyList(objType)) { BeerXMLProperty beerXmlProperty = typeProperty.Value; string propertyName = beerXmlProperty.Name; object propertyValue = beerXmlProperty.Property.GetValue(obj); if (ShouldAddProperty(beerXmlProperty.Attribute, propertyValue)) { if (beerXmlProperty.IsIBeerXMLEntity) { element.Add(GetBeerXElement((IBeerXMLEntity)propertyValue)); } else { element.Add(new XElement(propertyName, GetStringFromProperty(beerXmlProperty.Property.PropertyType, propertyValue))); } } } return(element); }
/// <summary> /// Parses an IBeerXMLEntity from the given XElement /// </summary> /// <param name="element">The element.</param> /// <returns></returns> private IBeerXMLEntity GetEntityFromElement(XElement element) { // get the name of the child property string propertyName = element.Name.ToString(); Type objectType = BeerXMLProperty.TryGetTypeByName(propertyName); // throw if the name of this tag does not have a corresponding // BeerXML type if (objectType == null) { if (this.StrictModeEnabled) { throw new BeerXMLUnknownTypeTagException(propertyName, string.Format("Tag with name [{0}] has no corresponding IBeerXMLEntity type!", propertyName)); } return(null); } // create the empty IBeerXMLEntity IBeerXMLEntity objectFromXElement = (IBeerXMLEntity)Activator.CreateInstance(objectType, nonPublic: true); // IRecordSets need to have their child records // deserialized and added to the collection if (objectFromXElement is IRecordSet) { IRecordSet objAsRecordSet = objectFromXElement as IRecordSet; foreach (XElement childElement in element.Elements()) { IRecord child = GetEntityFromElement(childElement) as IRecord; // could be null if it was an invalid tag. // Invalid tags are ignored if (child != null) { objAsRecordSet.Add(child); } } return(objAsRecordSet); } // get the dictionary of property name to property for this type IDictionary <string, BeerXMLProperty> propertyList = BeerXMLProperty.GetBeerXMLPropertyList(objectType); // foreach child element, set the property foreach (XElement childElement in element.Elements()) { string childName = childElement.Name.ToString(); // this means that the given tag is unrecognized - since the overall // property type is recognized, just ignore it if (!propertyList.ContainsKey(childName)) { continue; } BeerXMLProperty property = propertyList[childName]; SetPropertyByType(property, objectFromXElement, childElement); } return(objectFromXElement); }