internal static CollapseValueDefinition FromXElement(XElement element, CollapseContext context)
        {
            if (!element.IsCollapseValue())
                return null;

            XAttribute sourceAttribute = element.Attribute("source");

            if (sourceAttribute == null)
                throw new MissingSourceAttributeOnValueElementException();

            object obj = context.GetObject(sourceAttribute.Value.TrimmedSplit());

            String valueText = context.GetTextualRepresentation(obj);

            return new CollapseValueDefinition(valueText);
        }
示例#2
0
        /// <summary>
        /// Exports the <paramref name="rootData"/> as XML document, using the <paramref name="template"/> template.
        /// </summary>
        /// <exception cref="Kaisean.Tools.CollapseXml.Errors.InvalidXmlFormatException">XML document is not a valid XML.</exception>
        /// <exception cref="Kaisean.Tools.CollapseXml.Errors.MissingNamespaceException">Collapse namespace is missing from the template root element.</exception>
        /// <exception cref="Kaisean.Tools.CollapseXml.Errors.MultipleRootElementsException">Multiple root elements have been defined.</exception>
        /// <exception cref="Kaisean.Tools.CollapseXml.Errors.RootElementNotFoundException">A template does not contain a root element.</exception>
        /// <exception cref="Kaisean.Tools.CollapseXml.Errors.RootElementIsAForeachException">Root element in template contains cx:foreach attribute.</exception>
        /// <param name="rootData">Object to export</param>
        /// <param name="template">Collapse transformation template</param>
        /// <returns>Transformed XML filled with data, returned as an XDocument</returns>
        public XDocument ExportToXml(object rootData, string template)
        {
            // Create a context that contains the data provided
            context = new CollapseContext(rootData, filterMap, dateTimeFormat, numberFormatProvider);

            // A new document is created
            XDocument newDocument = new XDocument();

            // Get the template root element (root element in the resulting XML)
            XElement templateRoot = getXTemplate(template);

            // Create a frame for the template root and add it to the new document
            XElement newRootElement = templateRoot.AsXElementFrame();
            newDocument.Add(newRootElement);

            // Processes the node - in parallel : an original node and its frame
            processElement(templateRoot, newRootElement, null);

            // Return the created document
            return newDocument;
        }