/// <summary>method add child element to this element from source element or empty child element, if source child element is not found /// </summary> /// <param name="e">XElement instance</param> /// <param name="srcParentElement">source element with child element, which value is used for new element</param> /// <param name="srcChildElementName">child element name, which value is used for new element</param> /// <param name="destChildElementName">name of new element, default is source child element name</param> /// <returns>added child element</returns> public static XElement AddChildElement(this XElement e, XElement srcParentElement, string srcChildElementName, string destChildElementName = "") { string name = (destChildElementName == "" ? srcChildElementName : destChildElementName); XElement srcElement = srcParentElement.Element(srcChildElementName); if(srcElement != null) return e.AddChildElement(name, srcElement.Value); else return e.AddChildElement(name); }
/// <summary>method add child element to XML data class /// </summary> /// <param name="e">XElement instance</param> /// <param name="xmlDataDocument">source XML element</param> /// <param name="rootName">name of source XML parent collection element</param> /// <param name="itemName">name of source XML collection item element</param> /// <param name="srcItemChildName">name of source XML collection item's child element</param> /// <param name="destItemChildName">name of added XML element</param> /// <param name="whereItemChildName">optional name of source XML collection item's child element used for filtration</param> /// <param name="whereItemChildValue">optional value of source XML collection item's child element used for filtration</param> public static XElement AddChildElement(this XElement e, XElement xmlDataDocument, string rootName, string itemName, string srcItemChildName, string destItemChildName, string[] whereItemChildNames = null, string[] whereItemChildValues = null) { XElement childElement; XElement root = xmlDataDocument.Descendants(rootName).FirstOrDefault(); if (root != null) { XElement xElement = null; if (whereItemChildNames != null) { int whereCount = whereItemChildNames.GetLength(0); if (whereCount > 0) { List<XElement> query = root.Elements(itemName).Where((x) => x.Element(whereItemChildNames[0]).Value == whereItemChildValues[0]).ToList(); if (query.Count() > 0 && whereCount > 1) { for (int i = 1; i < whereCount; i++) { query = query.Where((x) => x.Element(whereItemChildNames[i]).Value == whereItemChildValues[i]).ToList(); if (query.Count() == 0) break; } } if (query.Count > 0) xElement = query[0]; else xElement = null; } else xElement = root.Elements(itemName).FirstOrDefault(); } else xElement = root.Elements(itemName).FirstOrDefault(); if (xElement != null) childElement = e.AddChildElement(destItemChildName, xElement.GetStringValue(srcItemChildName)); else childElement = e.AddChildElement(destItemChildName); } else childElement = e.AddChildElement(destItemChildName); return childElement; }
/// <summary>method add child element to this element from source element descendands or empty child element, if source child element is not found /// </summary> /// <param name="e">XElement instance</param> /// <param name="srcParentElement">source element with descendant elements, which value is used for new element</param> /// <param name="srcChildElementName">descendant element name, which value is used for new element</param> /// <param name="destChildElementName">name of added element, default is source child element name</param> /// <returns></returns> public static XElement AddFirstDescendantElement(this XElement e, XElement srcParentElement, string srcChildElementName, string destChildElementName = "") { string name = (destChildElementName == "" ? srcChildElementName : destChildElementName); XElement srcElement = srcParentElement.Descendants(srcChildElementName).FirstOrDefault() ; if (srcElement != null) return e.AddChildElement(name, srcElement.Value); else return e.AddChildElement(name); }
/// <summary>method add HL7 child element to this element if child element value is not empty /// </summary> /// <param name="e">XElement instance</param> /// <param name="сhildElementName">child element name</param> /// <param name="сhildElementValue">child element value</param> /// <returns>added child element if child element value is not empty, otherwise null</returns> public static XElement AddChildHl7Element(this XElement e, string сhildElementName, string сhildElementValue) { return (сhildElementValue == string.Empty ? null : e.AddChildElement(сhildElementName, сhildElementValue)); }