Sage.Modules.ModuleResult.AppendDataElement C# (CSharp) Method

AppendDataElement() public method

Creates a mod:data element in the current module.
If this method is invoked with no arguments, a mod:data element will be created (if it doesn't exist in the current module) and the final mod:data element will be returned. If an element whose name is mod:data is supplied, if will either be appended to the current module (if the module doesn't have a mod:data element already) or it will replace the existing mod:data element if one exists already. The element that will be returned will be the appended copy of the specified dataElement.
public AppendDataElement ( XmlNode dataElement = null ) : XmlElement
dataElement System.Xml.XmlNode Optional data element to add to the current module's element.
return System.Xml.XmlElement
        public XmlElement AppendDataElement(XmlNode dataElement = null)
        {
            return (XmlElement) this.AppendDataNode(dataElement);
        }

Usage Example

Beispiel #1
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            SageContext context = configuration.Context;
            ModuleResult result = new ModuleResult(moduleElement);

            XmlNode pathNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:path", nm);
            XmlNode selectionNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:xpath", nm);
            XmlNodeList namespaceNodes = moduleElement.SelectNodes("mod:config/mod:source/mod:namespaces/mod:namespace", nm);

            string xpath = selectionNode != null ? selectionNode.InnerText : null;

            if (pathNode == null)
            {
                result.Status = ModuleResultStatus.None;
                return result;
            }

            string path = pathNode.InnerText.Trim();
            if (string.IsNullOrEmpty(path))
            {
                log.WarnFormat("The path element shouldn't be empty.");
                result.Status = ModuleResultStatus.ModuleWarning;
                return result;
            }

            path = context.Path.Substitute(path);
            if (!Path.IsPathRooted(path))
            {
                string configDirectory = Path.GetDirectoryName(configuration.Info.ConfigPath);
                path = Path.Combine(configDirectory, path);
            }

            string resolved = context.Path.Resolve(path);
            if (!File.Exists(resolved))
            {
                log.WarnFormat("The specified xml path '{0}' ('{1}') doesn't exist", path, resolved);
                result.Status = ModuleResultStatus.ModuleWarning;
                return result;
            }

            XmlNode selection;
            try
            {
                selection = context.Resources.LoadXml(resolved);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Failed to add specified file '{0}' ('{1}') as XML document: {2}",
                    path, resolved, ex.Message);

                result.Status = ModuleResultStatus.ModuleError;
                return result;
            }

            if (!string.IsNullOrEmpty(xpath))
            {
                try
                {
                    XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
                    manager.AddNamespace("default", XmlNamespaces.XHtmlNamespace);

                    xpath = defaultNamespaceNodes.Replace(xpath, "$1default:$2$3");
                    foreach (XmlElement node in namespaceNodes)
                    {
                        string prefix = node.GetAttribute("prefix");
                        manager.AddNamespace(prefix, node.InnerText.Trim());
                    }

                    selection = selection.SelectSingleNode(xpath, manager);
                    if (selection == null)
                    {
                        log.WarnFormat("The xpath expression '{0}' yielded no result.", xpath);
                        result.Status = ModuleResultStatus.ModuleWarning;
                        return result;
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Attempting to select using xpath expression '{0}' resulted in error: {1}.",
                        xpath, ex.Message);

                    result.Status = ModuleResultStatus.ModuleError;
                    return result;
                }
            }
            else if (selectionNode != null)
            {
                log.WarnFormat("The selection element shouldn't be empty.");
                result.Status = ModuleResultStatus.ModuleWarning;
            }

            result.AppendDataElement(selection);
            return result;
        }
All Usage Examples Of Sage.Modules.ModuleResult::AppendDataElement