private void SerializeInnerResource(XmlWriter writer, LinkedResource resource)
        {
            var type = resource.GetType();
            DataContractElementWrite(writer, type);

            if (resource.Links.Any())
            {
                writer.WriteStartElement("links");
                foreach (var link in resource.Links)
                {
                    writer.WriteStartElement("link");
                    writer.WriteAttributeString("rel", link.Rel);
                    writer.WriteAttributeString("href", link.Href);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }

            SerializeProperties(writer, resource);

            writer.WriteEndElement();
        }
        private void SerializeInnerResource(XmlWriter writer, LinkedResource resource, string rel = null)
        {
            writer.WriteStartElement("resource");
            writer.WriteAttributeString("href", resource.Href);
            if (rel != null)
            {
                writer.WriteAttributeString("rel", rel);
            }

            foreach (var link in resource.Links)
            {
                writer.WriteStartElement("link");
                writer.WriteAttributeString("rel", link.Rel);
                writer.WriteAttributeString("href", link.Href);
                writer.WriteEndElement();
            }

            var properties = resource.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p =>
                    p.Name != "Href" &&
                    p.Name != "Links");

            foreach (var property in properties)
            {
                var propertyValue = property.GetValue(resource);
                if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string))
                {
                    writer.WriteElementString(property.Name, propertyValue.ToString());
                }
                else if (propertyValue is LinkedResource)
                {
                    SerializeInnerResource(writer, (LinkedResource)propertyValue, property.Name);
                }
            }

            writer.WriteEndElement();
        }
        private void SerializeProperties(XmlWriter writer, LinkedResource resource)
        {
            var type = resource.GetType();

            var properties = resource.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p =>
                    p.Name != "Href" &&
                    p.Name != "Links");

            foreach (var property in properties)
            {
                if (!property.GetCustomAttributes(typeof (DataMemberAttribute), true).Any())
                {
                    continue;
                }

                var propertyValue = property.GetValue(resource);
                var propertyName = GetPropertyName(property, type);
                if (property.PropertyType.IsPrimitive || property.PropertyType == typeof (string))
                {
                    writer.WriteElementString(propertyName, propertyValue.ToString());
                }
            }
        }