예제 #1
0
 /// <summary>
 /// Secures the document.
 /// </summary>
 /// <param name="xDoc">The x document.</param>
 /// <param name="bReturnCopy">if set to <c>true</c> [b return copy].</param>
 /// <param name="nodes">The nodes.</param>
 /// <param name="attributes">The attributes.</param>
 /// <returns></returns>
 public XmlDocument SecureDocument(XmlDocument xDoc, bool bReturnCopy, XmlSecureXPaths nodes, XmlSecureXPaths attributes)
 {
     return((XmlDocument)SecureNode(xDoc, bReturnCopy, nodes, attributes));
 }
예제 #2
0
        /// <summary>
        /// Secures the node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="bReturnCopy">if set to <c>true</c> [b return copy].</param>
        /// <param name="nodes">The nodes.</param>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        public XmlNode SecureNode(XmlNode node, bool bReturnCopy, XmlSecureXPaths nodes, XmlSecureXPaths attributes)
        {
            if (node == null)
            {
                return(null);
            }

            var nodeCopy = bReturnCopy ? node.CloneNode(true) : node;
            var xDoc     = nodeCopy.OwnerDocument;

            if (xDoc == null)
            {
                xDoc = (XmlDocument)nodeCopy;
            }

            XmlNamespaceManager nsmgr;

            lock (_xmlNamespaceManagers)
            {
                nsmgr = new XmlNamespaceManager(xDoc.NameTable);
                foreach (NamespaceManagerInfo nsInfo in _xmlNamespaceManagers.Values)
                {
                    nsmgr.AddNamespace(nsInfo.Prefix, nsInfo.Uri);
                }
            }

            if (nodes != null)
            {
                foreach (XmlSecureNodeXPath nInfo in nodes.XPaths.Values)
                {
                    foreach (string xPath in nInfo.AppliedXPaths)
                    {
                        var selectedNodes = SelectNodes(nodeCopy, xPath, nsmgr);
                        if (selectedNodes == null || selectedNodes.Count == 0)
                        {
                            continue;
                        }

                        foreach (XmlNode sNode in selectedNodes)
                        {
                            sNode.InnerText = nInfo.HashString;
                        }
                    }
                }
            }

            if (attributes != null)
            {
                foreach (XmlSecureAttributeXPath aInfo in attributes.XPaths.Values)
                {
                    foreach (string xPath in aInfo.AppliedXPaths)
                    {
                        var selectedNodes = SelectNodes(nodeCopy, xPath, nsmgr);
                        if (selectedNodes == null || selectedNodes.Count == 0)
                        {
                            continue;
                        }
                        foreach (XmlNode sNode in selectedNodes)
                        {
                            if (sNode.Attributes == null || sNode.Attributes.Count == 0)
                            {
                                continue;
                            }
                            foreach (string attrName in aInfo.Attributes)
                            {
                                var attr = sNode.Attributes[attrName];
                                if (attr != null)
                                {
                                    attr.InnerText = aInfo.HashString;
                                }
                            }
                        }
                    }
                }
            }

            return(nodeCopy);
        }