static void WriteHalResource(Representation representation, XmlWriter writer, string propertyName = null)
        {
            if (representation == null)
            {
                return;
            }

            representation.RepopulateHyperMedia();

            // First write the well-known HAL properties
            writer.WriteStartElement("resource");
            writer.WriteAttributeString("rel", representation.Rel);
            writer.WriteAttributeString("href", representation.Href);
            if (representation.LinkName != null || propertyName != null)
            {
                writer.WriteAttributeString("name", representation.LinkName = representation.LinkName ?? propertyName);
            }

            // Second, determine if resource is of Generic Resource List Type , list out all the items
            var representationList = representation as IRepresentationList;

            if (representationList != null)
            {
                foreach (var item in representationList.Cast <Representation>())
                {
                    WriteHalResource(item, writer);
                }
            }

            //Third write out the links of the resource
            var links = new HashSet <Link>(representation.Links.Where(link => link.Rel != "self"), new LinkEqualityComparer());

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

            // Fourth, write the rest of the properties
            WriteResourceProperties(representation, writer);

            writer.WriteEndElement();
        }
        static void WriteHalResource(Representation representation, XmlWriter writer, string propertyName = null)
        {
            if (representation == null)
            {
                return;
            }

            representation.RepopulateHyperMedia();

            // First write the well-known HAL properties
            writer.WriteStartElement("resource");
            writer.WriteAttributeString("rel", representation.Rel);
            writer.WriteAttributeString("href", representation.Href);
            if (representation.LinkName != null || propertyName != null)
            {
                writer.WriteAttributeString("name", representation.LinkName = representation.LinkName ?? propertyName);
            }

            // Second, determine if resource is of Generic Resource List Type , list out all the items
            var representationList = representation as IRepresentationList;
            if (representationList != null)
            {
                foreach (var item in representationList.Cast<Representation>())
                {
                    WriteHalResource(item, writer);
                }
            }

            //Third write out the links of the resource
            foreach (var link in representation.Links.Where(link => link.Rel != "self"))
            {
                writer.WriteStartElement("link");
                writer.WriteAttributeString("rel", link.Rel);
                writer.WriteAttributeString("href", link.Href);
                writer.WriteEndElement();
            }

            // Fourth, write the rest of the properties
            WriteResourceProperties(representation, writer);

            writer.WriteEndElement();
        }