/// <summary> /// Returns the requested node and optionally creates it if it does not exist. /// </summary> /// <param name="document">XmlDocument to get the node from.</param> /// <param name="path">Path for the node.</param> /// <param name="emptyNode">Specifies whether to create a node if it does not exist.</param> /// <returns></returns> public static XmlNode GetNode(XmlDocument document, string path, EmptyNodeHandling emptyNode) { if (document != null) { if (document.ChildNodes.Count == 0) { if (emptyNode == EmptyNodeHandling.CreateNew) { int indexOfSlash = path.IndexOf("/", System.StringComparison.Ordinal); string rootNodeName = path.Substring(0, indexOfSlash); XmlNode rootNode = document.CreateElement(rootNodeName); document.AppendChild(rootNode); path = path.Substring(indexOfSlash + 1); } else { return(null); } } return(GetNode(document.DocumentElement, path, emptyNode)); } // If document is empty, create the root node return(null); }
private static void DoMergeXml(XmlNode baseXmlNode, XmlNode mergeXmlNode, params string[] uniqueIdentifiers) { foreach (XmlNode xmlNode in mergeXmlNode) { if (xmlNode.Name != "#text") { EmptyNodeHandling emptyNode = Common.StringArrayContains(uniqueIdentifiers, xmlNode.Name) ? EmptyNodeHandling.ForceCreateNew : EmptyNodeHandling.CreateNew; XmlNode currentBaseXmlnode = GetNode(baseXmlNode, xmlNode.Name, emptyNode); if (xmlNode.Attributes != null) { foreach (XmlAttribute xmlAttribute in xmlNode.Attributes) { SetAttributeValue(currentBaseXmlnode, xmlAttribute.Name, xmlAttribute.Value); } } if (xmlNode.HasChildNodes) { DoMergeXml(currentBaseXmlnode, xmlNode, uniqueIdentifiers); } } else { XmlNode textNode = baseXmlNode.SelectSingleNode("text()"); if (textNode != null) { textNode.InnerText = xmlNode.Value; } else if (baseXmlNode.OwnerDocument != null) { baseXmlNode.AppendChild(baseXmlNode.OwnerDocument.CreateTextNode(xmlNode.Value)); } } } }
protected XmlNode GetNode(string cleanPath, EmptyNodeHandling emptyNode) { return(CommonXml.GetNode(m_ParentNode, cleanPath, emptyNode)); }
/// <summary> /// Returns the requested node and optionally creates it if it does not exist. /// </summary> /// <param name="fromXmlNode">From XML node.</param> /// <param name="path">Path for the node.</param> /// <param name="emptyNode">Specifies whether to create a node if it does not exist.</param> /// <returns></returns> public static XmlNode GetNode(XmlNode fromXmlNode, string path, EmptyNodeHandling emptyNode) { string[] pathParts = path.Split('/'); pathParts[0] = RenameIntegerPath(pathParts[0]); XmlNode xmlNode = null; if (emptyNode != EmptyNodeHandling.ForceCreateNew) { xmlNode = pathParts[0] == string.Empty ? fromXmlNode : fromXmlNode.SelectSingleNode(pathParts[0]); } else { if (fromXmlNode.OwnerDocument != null) { xmlNode = fromXmlNode.OwnerDocument.CreateElement(pathParts[0]); fromXmlNode.AppendChild(xmlNode); } } if (xmlNode == null) { if (emptyNode == EmptyNodeHandling.CreateNew) { if (fromXmlNode.OwnerDocument != null) { xmlNode = fromXmlNode.OwnerDocument.CreateElement(pathParts[0]); fromXmlNode.AppendChild(xmlNode); } } else if (emptyNode == EmptyNodeHandling.Ignore) { return(null); } } for (int i = 1; i < pathParts.Length; i++) { string pathPart = pathParts[i]; pathPart = RenameIntegerPath(pathPart); if (pathPart != string.Empty) { XmlNode xmlOldNode = xmlNode; if (emptyNode != EmptyNodeHandling.ForceCreateNew) { if (xmlOldNode != null) { xmlNode = xmlOldNode.SelectSingleNode(pathPart); } else { if (fromXmlNode.OwnerDocument != null) { xmlNode = fromXmlNode.OwnerDocument.CreateElement(pathParts[0]); fromXmlNode.AppendChild(xmlNode); } } } if (xmlNode == null) { if (emptyNode == EmptyNodeHandling.CreateNew) { if (fromXmlNode.OwnerDocument != null) { xmlNode = fromXmlNode.OwnerDocument.CreateElement(pathPart); if (xmlOldNode != null) { xmlOldNode.AppendChild(xmlNode); } } } else if (emptyNode == EmptyNodeHandling.Ignore) { break; } } } } return(xmlNode); }
protected XmlNode GetNode(string cleanPath, EmptyNodeHandling emptyNode) { XmlNode xmlNode = CommonXml.GetNode(_parentNode, cleanPath, emptyNode); return(xmlNode); }