Example #1
0
        public void Write(MetadataBuilder builder, Stream stream)
        {
            var document = new JObject();

            foreach (var pair in builder.GetValues())
            {
                SetValue(document, pair.Key, pair.Value);
            }

            foreach (var section in builder.GetSections())
            {
                var sectionEl = new JObject();
                document[section.Name] = sectionEl;

                if (string.IsNullOrEmpty(section.GroupByProperty))
                {
                    foreach (var item in section.GetEntries())
                    {
                        var el = new JObject();
                        sectionEl[section.ItemName] = el;
                        foreach (var pair in item.GetValues())
                        {
                            SetValue(el, pair.Key, pair.Value);
                        }
                    }
                }
                else
                {
                    foreach (var group in section.GetEntries().GroupBy(s => s.GetMetadataValue(section.GroupByProperty)))
                    {
                        var groupEl = new JObject();
                        sectionEl[group.Key] = groupEl;

                        foreach (var item in group)
                        {
                            var el = new JObject();
                            groupEl[section.ItemName] = el;
                            foreach (var pair in item.GetValues())
                            {
                                if (string.Equals(pair.Key, section.GroupByProperty, StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }

                                SetValue(el, pair.Key, pair.Value);
                            }
                        }
                    }
                }
            }

            var sw = new StreamWriter(stream) { AutoFlush = true };
            sw.Write(document.ToString(Formatting.Indented));
        }
Example #2
0
        public void Save(MetadataBuilder builder, Stream stream)
        {
            var metadata = new XElement("metadata");

            foreach (var pair in builder.GetValues())
            {
                if (HasValue(pair.Value))
                {
                    metadata.Add(new XElement(pair.Key, pair.Value));
                }
            }

            var sectionElements = new List<XElement>();

            foreach (var section in builder.GetSections())
            {
                var sectionEl = new XElement(section.Name);

                if (string.IsNullOrEmpty(section.GroupByProperty))
                {
                    foreach (var item in section.GetEntries())
                    {
                        var el = new XElement(section.ItemName);
                        foreach (var pair in item.GetValues())
                        {
                            el.Add(new XAttribute(pair.Key, pair.Value));
                        }
                        sectionEl.Add(el);
                    }
                }
                else
                {
                    foreach (var group in section.GetEntries().GroupBy(s => s.GetMetadataValue(section.GroupByProperty)))
                    {
                        var groupEl = new XElement("group", new XAttribute(section.GroupByProperty, group.Key));
                        foreach (var item in group)
                        {
                            var el = new XElement(section.ItemName);
                            foreach (var pair in item.GetValues())
                            {
                                if (string.Equals(pair.Key, section.GroupByProperty, StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }

                                el.Add(new XAttribute(pair.Key, pair.Value?.ToString() ?? ""));
                            }
                            groupEl.Add(el);
                        }

                        sectionEl.Add(groupEl);
                    }
                }

                sectionElements.Add(sectionEl);
            }

            var document = new XDocument(
                new XElement("package",
                    metadata));

            sectionElements.ForEach(el => metadata.Add(el));

            document.Save(stream);
        }