public static void WriteTo(Bundle bundle, XmlWriter writer, bool summary = false)
        {
            if (bundle == null)
            {
                throw new ArgumentException("Bundle cannot be null");
            }

            var root = new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_FEED);

            if (!String.IsNullOrWhiteSpace(bundle.Title))
            {
                root.Add(xmlCreateTitle(bundle.Title));
            }
            if (SerializationUtil.UriHasValue(bundle.Id))
            {
                root.Add(xmlCreateId(bundle.Id));
            }
            if (bundle.LastUpdated != null)
            {
                root.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_UPDATED, bundle.LastUpdated));
            }

            if (!String.IsNullOrWhiteSpace(bundle.AuthorName))
            {
                root.Add(xmlCreateAuthor(bundle.AuthorName, bundle.AuthorUri));
            }
            if (bundle.TotalResults != null)
            {
                root.Add(new XElement(BundleXmlParser.XOPENSEARCHNS + BundleXmlParser.XATOM_TOTALRESULTS, bundle.TotalResults));
            }

            if (bundle.Links != null)
            {
                foreach (var l in bundle.Links)
                {
                    root.Add(xmlCreateLink(l.Rel, l.Uri));
                }
            }

            if (bundle.Tags != null)
            {
                foreach (var tag in bundle.Tags)
                {
                    root.Add(TagListSerializer.CreateTagCategoryPropertyXml(tag));
                }
            }

            foreach (var entry in bundle.Entries)
            {
                root.Add(createEntry(entry, summary));
            }

            root.WriteTo(writer);
            //var result = new XDocument(root);
            //result.WriteTo(writer);
        }
        public static void WriteTo(Bundle bundle, JsonWriter writer, bool summary = false)
        {
            if (bundle == null)
            {
                throw new ArgumentException("Bundle cannot be null");
            }

            JObject result = new JObject();

            result.Add(new JProperty(JsonDomFhirReader.RESOURCETYPE_MEMBER_NAME, "Bundle"));

            if (!String.IsNullOrWhiteSpace(bundle.Title))
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_TITLE, bundle.Title));
            }
            if (SerializationUtil.UriHasValue(bundle.Id))
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_ID, bundle.Id));
            }
            if (bundle.LastUpdated != null)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_UPDATED, bundle.LastUpdated));
            }

            if (!String.IsNullOrWhiteSpace(bundle.AuthorName))
            {
                result.Add(jsonCreateAuthor(bundle.AuthorName, bundle.AuthorUri));
            }
            if (bundle.TotalResults != null)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_TOTALRESULTS, bundle.TotalResults.ToString()));
            }

            if (bundle.Links.Count > 0)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_LINK, jsonCreateLinkArray(bundle.Links)));
            }
            if (bundle.Tags != null && bundle.Tags.Count() > 0)
            {
                result.Add(TagListSerializer.CreateTagCategoryPropertyJson(bundle.Tags));
            }

            var entryArray = new JArray();

            foreach (var entry in bundle.Entries)
            {
                entryArray.Add(createEntry(entry, summary));
            }

            result.Add(new JProperty(BundleXmlParser.XATOM_ENTRY, entryArray));

            result.WriteTo(writer);
        }
        private static JObject createEntry(BundleEntry entry, bool summary)
        {
            JObject result = new JObject();

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (!String.IsNullOrEmpty(re.Title))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_TITLE, re.Title));
                }
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));
                }

                if (re.LastUpdated != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_UPDATED, re.LastUpdated));
                }
                if (re.Published != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_PUBLISHED, re.Published));
                }

                if (!String.IsNullOrWhiteSpace(re.AuthorName))
                {
                    result.Add(jsonCreateAuthor(re.AuthorName, re.AuthorUri));
                }
            }
            else
            {
                DeletedEntry de = (DeletedEntry)entry;
                if (de.When != null)
                {
                    result.Add(new JProperty(BundleJsonParser.JATOM_DELETED, de.When));
                }
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));
                }
            }

            if (entry.Links != null && entry.Links.Count() > 0)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_LINK, jsonCreateLinkArray(entry.Links)));
            }

            if (entry.Tags != null && entry.Tags.Count() > 0)
            {
                result.Add(TagListSerializer.CreateTagCategoryPropertyJson(entry.Tags));
            }

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (re.Resource != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_CONTENT,
                                             getContentsAsJObject(re.Resource, summary)));
                }

                // Note: this is a read-only property, so it is serialized but never parsed
                if (entry.Summary != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_SUMMARY, entry.Summary));
                }
            }

            return(result);
        }
        private static XElement createEntry(BundleEntry entry, bool summary)
        {
            XElement result = null;

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                result = new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_ENTRY);

                if (!String.IsNullOrEmpty(re.Title))
                {
                    result.Add(xmlCreateTitle(re.Title));
                }
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(xmlCreateId(entry.Id));
                }

                if (re.LastUpdated != null)
                {
                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_UPDATED, re.LastUpdated.Value));
                }
                if (re.Published != null)
                {
                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_PUBLISHED, re.Published.Value));
                }

                if (!String.IsNullOrWhiteSpace(re.AuthorName))
                {
                    result.Add(xmlCreateAuthor(re.AuthorName, re.AuthorUri));
                }
            }
            else
            {
                result = new XElement(BundleXmlParser.XTOMBSTONE + BundleXmlParser.XATOM_DELETED_ENTRY);
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(new XAttribute(BundleXmlParser.XATOM_DELETED_REF, entry.Id.ToString()));
                }
                if (((DeletedEntry)entry).When != null)
                {
                    result.Add(new XAttribute(BundleXmlParser.XATOM_DELETED_WHEN, ((DeletedEntry)entry).When));
                }
            }

            if (entry.Links != null)
            {
                foreach (var l in entry.Links)
                {
                    if (l.Uri != null)
                    {
                        result.Add(xmlCreateLink(l.Rel, l.Uri));
                    }
                }
            }

            if (entry.Tags != null)
            {
                foreach (var tag in entry.Tags)
                {
                    result.Add(TagListSerializer.CreateTagCategoryPropertyXml(tag));
                }
            }

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (re.Resource != null)
                {
                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_CONTENT,
                                            new XAttribute(BundleXmlParser.XATOM_CONTENT_TYPE, "text/xml"),
                                            getContentAsXElement(re.Resource, summary)));
                }

                // Note: this is a read-only property, so it is serialized but never parsed
                if (entry.Summary != null)
                {
                    var xelem = XElement.Parse(entry.Summary);
                    xelem.Name = XHtml.XHTMLNS + xelem.Name.LocalName;

                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_SUMMARY,
                                            new XAttribute(BundleXmlParser.XATOM_CONTENT_TYPE, "xhtml"), xelem));
                }
            }

            return(result);
        }