internal XmlNode GetOrCreateExtLstSubNode(string uriGuid, string prefix, string[] uriOrder = null) { foreach (XmlElement node in GetNodes("d:extLst/d:ext")) { if (node.Attributes["uri"].Value.Equals(uriGuid, StringComparison.OrdinalIgnoreCase)) { return(node); } } var extLst = (XmlElement)CreateNode("d:extLst"); XmlElement prependChild = null; if (uriOrder != null) { foreach (var child in extLst.ChildNodes) { if (child is XmlElement e) { var uo1 = Array.IndexOf(uriOrder, e.GetAttribute("uri")); var uo2 = Array.IndexOf(uriOrder, uriGuid); if (uo1 > uo2) { prependChild = e; } } } } var newExt = TopNode.OwnerDocument.CreateElement("ext", ExcelPackage.schemaMain); if (!string.IsNullOrEmpty(prefix)) { newExt.SetAttribute($"xmlns:{prefix}", NameSpaceManager.LookupNamespace(prefix)); } newExt.SetAttribute("uri", uriGuid); if (prependChild == null) { extLst.AppendChild(newExt); } else { extLst.InsertBefore(newExt, prependChild); } return(newExt); }
public static void LoadReferencesToTree(TreeView projectView, MapReferences newRef, ContextMenuStrip mapMenu) { MapReferences pseudoRef = null; if (!FindMap(projectView, newRef.ViableInfo.TheMap.ClassName, out pseudoRef)) { TreeNode aNode = projectView.Nodes[0].Nodes[3].Nodes.Add(NameSpaceManager.RemoveNameSpace(newRef.ViableInfo.TheMap.ClassName)); aNode.Tag = newRef; aNode.ToolTipText = newRef.ViableInfo.TheMap.ClassName; aNode.ContextMenuStrip = mapMenu; projectView.SelectedNode = aNode; } else { projectView.SelectedNode.Tag = newRef; projectView.SelectedNode.ToolTipText = newRef.ViableInfo.TheMap.ClassName; } }
internal void RenameNode(XmlNode node, string prefix, string newName, string[] allowedChildren = null) { var doc = node.OwnerDocument; var newNode = doc.CreateElement(prefix, newName, NameSpaceManager.LookupNamespace(prefix)); while (TopNode.ChildNodes.Count > 0) { if (allowedChildren == null || allowedChildren.Contains(TopNode.ChildNodes[0].LocalName)) { newNode.AppendChild(TopNode.ChildNodes[0]); } else { TopNode.RemoveChild(TopNode.ChildNodes[0]); } } TopNode.ParentNode.ReplaceChild(newNode, TopNode); TopNode = newNode; }
/// <summary> /// Constructor /// </summary> /// <param name="worksheet">worksheet that owns the validation</param> /// <param name="itemElementNode">Xml top node (dataValidations) when importing xml</param> /// <param name="validationType">Data validation type</param> /// <param name="address">address for data validation</param> /// <param name="namespaceManager">Xml Namespace manager</param> internal ExcelDataValidation(ExcelWorksheet worksheet, string address, ExcelDataValidationType validationType, XmlNode itemElementNode, XmlNamespaceManager namespaceManager) : base(namespaceManager != null ? namespaceManager : worksheet.NameSpaceManager) { Require.Argument(address).IsNotNullOrEmpty("address"); address = CheckAndFixRangeAddress(address); if (itemElementNode == null) { //var xmlDoc = worksheet.WorksheetXml; TopNode = worksheet.WorksheetXml.SelectSingleNode("//d:dataValidations", worksheet.NameSpaceManager); // did not succeed using the XmlHelper methods here... so I'm creating the new node using XmlDocument... var nsUri = NameSpaceManager.LookupNamespace("d"); //itemElementNode = TopNode.OwnerDocument.CreateElement(_itemElementNodeName, nsUri); itemElementNode = TopNode.OwnerDocument.CreateElement(_itemElementNodeName.Split(':')[1], nsUri); TopNode.AppendChild(itemElementNode); } TopNode = itemElementNode; ValidationType = validationType; Address = new ExcelAddress(address); Init(); }
internal ExcelThemeBase(ExcelPackage package, XmlNamespaceManager nsm, ZipPackageRelationship rel, string path) : base(nsm, null) { ThemeUri = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri); Part = package.ZipPackage.GetPart(ThemeUri); RelationshipId = rel.Id; ThemeXml = new XmlDocument(); LoadXmlSafe(ThemeXml, Part.GetStream()); TopNode = ThemeXml.DocumentElement; _colorSchemePath = string.Format(_colorSchemePath, path); _fontSchemePath = string.Format(_fontSchemePath, path); _fmtSchemePath = string.Format(_fmtSchemePath, path); _pck = package; _hashes = new Dictionary <string, HashInfo>(); if (!NameSpaceManager.HasNamespace("a")) { NameSpaceManager.AddNamespace("a", ExcelPackage.schemaDrawings); } }
internal XmlNode ReplaceElement(XmlNode oldChild, string newNodeName) { var newNameSplit = newNodeName.Split(':'); XmlElement newElement; if (newNodeName.Length > 1) { var prefix = newNameSplit[0]; var name = newNameSplit[1]; var ns = NameSpaceManager.LookupNamespace(prefix); newElement = oldChild.OwnerDocument.CreateElement(newNodeName, ns); } else { newElement = oldChild.OwnerDocument.CreateElement(newNodeName, NameSpaceManager.DefaultNamespace); } oldChild.ParentNode.ReplaceChild(newElement, oldChild); return(newElement); }
public static MapReferences LoadMapToTree(TreeView projectView, MapedObject aMap, ContextMenuStrip mapMenu) { MapReferences newRef = null; if (!FindMap(projectView, aMap.ClassName, out newRef)) { newRef = new MapReferences(); newRef.ViableInfo = new ViableMapAndData(); newRef.ViableInfo.TheMap = aMap; TreeNode aNode = projectView.Nodes[0].Nodes[3].Nodes.Add(NameSpaceManager.RemoveNameSpace(aMap.ClassName)); aNode.Tag = newRef; aNode.ToolTipText = aMap.ClassName; aNode.ContextMenuStrip = mapMenu; projectView.SelectedNode = aNode; } return(newRef); }
/// <summary> /// Creates complex XML nodes /// </summary> /// <remarks> /// 1. "d:conditionalFormatting" /// 1.1. Creates/find the first "conditionalFormatting" node /// /// 2. "d:conditionalFormatting/@sqref" /// 2.1. Creates/find the first "conditionalFormatting" node /// 2.2. Creates (if not exists) the @sqref attribute /// /// 3. "d:conditionalFormatting/@id='7'/@sqref='A9:B99'" /// 3.1. Creates/find the first "conditionalFormatting" node /// 3.2. Creates/update its @id attribute to "7" /// 3.3. Creates/update its @sqref attribute to "A9:B99" /// /// 4. "d:conditionalFormatting[@id='7']/@sqref='X1:X5'" /// 4.1. Creates/find the first "conditionalFormatting" node with @id=7 /// 4.2. Creates/update its @sqref attribute to "X1:X5" /// /// 5. "d:conditionalFormatting[@id='7']/@id='8'/@sqref='X1:X5'/d:cfRule/@id='AB'" /// 5.1. Creates/find the first "conditionalFormatting" node with @id=7 /// 5.2. Set its @id attribute to "8" /// 5.2. Creates/update its @sqref attribute and set it to "X1:X5" /// 5.3. Creates/find the first "cfRule" node (inside the node) /// 5.4. Creates/update its @id attribute to "AB" /// /// 6. "d:cfRule/@id=''" /// 6.1. Creates/find the first "cfRule" node /// 6.1. Remove the @id attribute /// </remarks> /// <param name="topNode"></param> /// <param name="path"></param> /// <param name="nodeInsertOrder"></param> /// <param name="referenceNode"></param> /// <returns>The last node creates/found</returns> internal XmlNode CreateComplexNode( XmlNode topNode, string path, eNodeInsertOrder nodeInsertOrder, XmlNode referenceNode) { // Path is obrigatory if ((path == null) || (path == string.Empty)) { return(topNode); } XmlNode node = topNode; string nameSpaceURI = string.Empty; //TODO: BUG: when the "path" contains "/" in an attrribue value, it gives an error. // Separate the XPath to Nodes and Attributes foreach (string subPath in path.Split('/')) { // The subPath can be any one of those: // nodeName // x:nodeName // nodeName[find criteria] // x:nodeName[find criteria] // @attribute // @attribute='attribute value' // Check if the subPath has at least one character if (subPath.Length > 0) { // Check if the subPath is an attribute (with or without value) if (subPath.StartsWith("@")) { // @attribute --> Create attribute // @attribute='' --> Remove attribute // @attribute='attribute value' --> Create attribute + update value string[] attributeSplit = subPath.Split('='); string attributeName = attributeSplit[0].Substring(1, attributeSplit[0].Length - 1); string attributeValue = null; // Null means no attribute value // Check if we have an attribute value to set if (attributeSplit.Length > 1) { // Remove the ' or " from the attribute value attributeValue = attributeSplit[1].Replace("'", "").Replace("\"", ""); } // Get the attribute (if exists) XmlAttribute attribute = (XmlAttribute)(node.Attributes.GetNamedItem(attributeName)); // Remove the attribute if value is empty (not null) if (attributeValue == string.Empty) { // Only if the attribute exists if (attribute != null) { node.Attributes.Remove(attribute); } } else { // Create the attribue if does not exists if (attribute == null) { // Create the attribute attribute = node.OwnerDocument.CreateAttribute( attributeName); // Add it to the current node node.Attributes.Append(attribute); } // Update the attribute value if (attributeValue != null) { node.Attributes[attributeName].Value = attributeValue; } } } else { // nodeName // x:nodeName // nodeName[find criteria] // x:nodeName[find criteria] // Look for the node (with or without filter criteria) XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager); // Check if the node does not exists if (subNode == null) { string nodeName; string nodePrefix; string[] nameSplit = subPath.Split(':'); nameSpaceURI = string.Empty; // Check if the name has a prefix like "d:nodeName" if (nameSplit.Length > 1) { nodePrefix = nameSplit[0]; nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix); nodeName = nameSplit[1]; } else { nodePrefix = string.Empty; nameSpaceURI = string.Empty; nodeName = nameSplit[0]; } // Check if we have a criteria part in the node name if (nodeName.IndexOf("[") > 0) { // remove the criteria from the node name nodeName = nodeName.Substring(0, nodeName.IndexOf("[")); } if (nodePrefix == string.Empty) { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { if (node.OwnerDocument != null && node.OwnerDocument.DocumentElement != null && node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI && node.OwnerDocument.DocumentElement.Prefix == string.Empty) { subNode = node.OwnerDocument.CreateElement( nodeName, nameSpaceURI); } else { subNode = node.OwnerDocument.CreateElement( nodePrefix, nodeName, nameSpaceURI); } } // Check if we need to use the "SchemaOrder" if (nodeInsertOrder == eNodeInsertOrder.SchemaOrder) { // Check if the Schema Order List is empty if ((SchemaNodeOrder == null) || (SchemaNodeOrder.Length == 0)) { // Use the "Insert Last" option when Schema Order List is empty nodeInsertOrder = eNodeInsertOrder.Last; } else { // Find the prepend node in order to insert referenceNode = GetPrependNode(nodeName, node); if (referenceNode != null) { nodeInsertOrder = eNodeInsertOrder.Before; } else { nodeInsertOrder = eNodeInsertOrder.Last; } } } switch (nodeInsertOrder) { case eNodeInsertOrder.After: node.InsertAfter(subNode, referenceNode); referenceNode = null; break; case eNodeInsertOrder.Before: node.InsertBefore(subNode, referenceNode); referenceNode = null; break; case eNodeInsertOrder.First: node.PrependChild(subNode); break; case eNodeInsertOrder.Last: node.AppendChild(subNode); break; } } // Make the newly created node the top node when the rest of the path // is being evaluated. So newly created nodes will be the children of the // one we just created. node = subNode; } } } // Return the last created/found node return(node); }
/// <summary> /// Create the node path. Nodesa are inserted according to the Schema node oreder /// </summary> /// <param name="path">The path to be created</param> /// <param name="insertFirst">Insert as first child</param> /// <param name="addNew">Always add a new item at the last level.</param> /// <returns></returns> internal XmlNode CreateNode(string path, bool insertFirst, bool addNew = false) { XmlNode node = TopNode; XmlNode prependNode = null; if (path.StartsWith("/")) { path = path.Substring(1); } var subPaths = path.Split('/'); for (int i = 0; i < subPaths.Length; i++) { string subPath = subPaths[i]; XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager); if (subNode == null || (i == subPath.Length - 1 && addNew)) { string nodeName; string nodePrefix; string nameSpaceURI = ""; string[] nameSplit = subPath.Split(':'); if (SchemaNodeOrder != null && subPath[0] != '@') { insertFirst = false; prependNode = GetPrependNode(subPath, node); } if (nameSplit.Length > 1) { nodePrefix = nameSplit[0]; if (nodePrefix[0] == '@') { nodePrefix = nodePrefix.Substring(1, nodePrefix.Length - 1); } nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix); nodeName = nameSplit[1]; } else { nodePrefix = ""; nameSpaceURI = ""; nodeName = nameSplit[0]; } if (subPath.StartsWith("@")) { XmlAttribute addedAtt = node.OwnerDocument.CreateAttribute(subPath.Substring(1, subPath.Length - 1), nameSpaceURI); //nameSpaceURI node.Attributes.Append(addedAtt); } else { if (nodePrefix == "") { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { if (nodePrefix == "" || (node.OwnerDocument != null && node.OwnerDocument.DocumentElement != null && node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI && node.OwnerDocument.DocumentElement.Prefix == "")) { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { subNode = node.OwnerDocument.CreateElement(nodePrefix, nodeName, nameSpaceURI); } } if (prependNode != null) { node.InsertBefore(subNode, prependNode); prependNode = null; } else if (insertFirst) { node.PrependChild(subNode); } else { node.AppendChild(subNode); } } } node = subNode; } return(node); }
/// <summary> /// Create the node path. Nodes are inserted according to the Schema node order /// </summary> /// <param name="path">The path to be created</param> /// <param name="insertFirst">Insert as first child</param> /// <param name="addNew">Always add a new item at the last level.</param> /// <param name="exitName">Exit if after this named node has been created</param> /// <returns></returns> internal XmlNode CreateNode(string path, bool insertFirst, bool addNew = false, string exitName = "") { XmlNode node = TopNode; XmlNode prependNode = null; int lastUsedOrderIndex = 0; if (path.StartsWith("/")) { path = path.Substring(1); } var subPaths = path.Split('/'); for (int i = 0; i < subPaths.Length; i++) { string subPath = subPaths[i]; XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager); if (subNode == null || (i == subPath.Length - 1 && addNew)) { string nodeName; string nodePrefix; string nameSpaceURI = ""; string[] nameSplit = subPath.Split(':'); if (SchemaNodeOrder != null && subPath[0] != '@') { insertFirst = false; prependNode = GetPrependNode(subPath, node, ref lastUsedOrderIndex); } if (nameSplit.Length > 1) { nodePrefix = nameSplit[0]; if (nodePrefix[0] == '@') { nodePrefix = nodePrefix.Substring(1, nodePrefix.Length - 1); } nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix); nodeName = nameSplit[1]; } else { nodePrefix = ""; nameSpaceURI = ""; nodeName = nameSplit[0]; } if (subPath.StartsWith("@")) { XmlAttribute addedAtt = node.OwnerDocument.CreateAttribute(subPath.Substring(1, subPath.Length - 1), nameSpaceURI); //nameSpaceURI node.Attributes.Append(addedAtt); } else { if (nodePrefix == "") { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { if (nodePrefix == "" || (node.OwnerDocument != null && node.OwnerDocument.DocumentElement != null && node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI && node.OwnerDocument.DocumentElement.Prefix == "")) { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { subNode = node.OwnerDocument.CreateElement(nodePrefix, nodeName, nameSpaceURI); } } if (prependNode != null) { node.InsertBefore(subNode, prependNode); prependNode = null; } else if (insertFirst) { node.PrependChild(subNode); } else { node.AppendChild(subNode); } } if (nodeName == exitName) { return(subNode); } } else if (_schemaNodeOrder != null && subPath != "..") //Parent node, node order should not change. Parent node (..) is only supported in the start of the xpath { var ix = GetNodePos(subNode.LocalName, lastUsedOrderIndex); if (ix >= 0) { lastUsedOrderIndex = GetIndex(ix); } } node = subNode; } return(node); }
private XmlNode CreateNode(XmlNode node, string path, bool insertFirst, bool addNew, string exitName, string alternateContentRequires = null) { XmlNode prependNode = null; int lastUsedOrderIndex = 0; if (path.StartsWith("/")) { path = path.Substring(1); } var subPaths = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < subPaths.Length; i++) { string subPath = subPaths[i]; XmlNode subNode = node.SelectSingleNode(subPath, NameSpaceManager); if (subNode == null || (i == subPaths.Length - 1 && addNew)) { string nodeName; string nodePrefix; string nameSpaceURI = ""; string[] nameSplit = subPath.Split(':'); if (SchemaNodeOrder != null && subPath[0] != '@') { insertFirst = false; prependNode = GetPrependNode(subPath, node, ref lastUsedOrderIndex); } if (nameSplit.Length > 1) { nodePrefix = nameSplit[0]; if (nodePrefix[0] == '@') { nodePrefix = nodePrefix.Substring(1, nodePrefix.Length - 1); } nameSpaceURI = NameSpaceManager.LookupNamespace(nodePrefix); nodeName = nameSplit[1]; } else { nodePrefix = ""; nameSpaceURI = ""; nodeName = nameSplit[0]; } if (subPath.StartsWith("@")) { XmlAttribute addedAtt = node.OwnerDocument.CreateAttribute(subPath.Substring(1, subPath.Length - 1), nameSpaceURI); //nameSpaceURI node.Attributes.Append(addedAtt); } else { if (nodePrefix == "") { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { if (nodePrefix == "" || (node.OwnerDocument != null && node.OwnerDocument.DocumentElement != null && node.OwnerDocument.DocumentElement.NamespaceURI == nameSpaceURI && node.OwnerDocument.DocumentElement.Prefix == "")) { subNode = node.OwnerDocument.CreateElement(nodeName, nameSpaceURI); } else { subNode = node.OwnerDocument.CreateElement(nodePrefix, nodeName, nameSpaceURI); } } if (string.IsNullOrEmpty(alternateContentRequires) == false) { var altNode = node.OwnerDocument.CreateElement("AlternateContent", ExcelPackage.schemaMarkupCompatibility); var choiceNode = node.OwnerDocument.CreateElement("Choice", ExcelPackage.schemaMarkupCompatibility); altNode.AppendChild(choiceNode); choiceNode.SetAttribute("Requires", alternateContentRequires); choiceNode.AppendChild(subNode); subNode = altNode; } if (prependNode != null) { node.InsertBefore(subNode, prependNode); prependNode = null; } else if (insertFirst) { node.PrependChild(subNode); } else { node.AppendChild(subNode); } } if (nodeName == exitName) { return(subNode); } } else if (SchemaNodeOrder != null && subPath != "..") //Parent node, node order should not change. Parent node (..) is only supported in the start of the xpath { var ix = GetNodePos(subNode.LocalName, lastUsedOrderIndex); if (ix >= 0) { lastUsedOrderIndex = GetIndex(ix); } } node = subNode; } return(node); }