public void AddPI(object doc, string data)
        {
            XmlDocument xmlDocument = InMetaXmlUtils.TryGetXmlDocument(doc);

            if (xmlDocument != null)
            {
                XmlNode firstChild = xmlDocument.FirstChild;
                if (firstChild != null && (firstChild.NodeType == XmlNodeType.ProcessingInstruction && firstChild.Name == "xml"))
                {
                    xmlDocument.RemoveChild(firstChild);
                }
                XmlProcessingInstruction processingInstruction = xmlDocument.CreateProcessingInstruction("xml", data);
                xmlDocument.InsertBefore((XmlNode)processingInstruction, xmlDocument.FirstChild);
            }
            else
            {
                object firstChild = InMetaXmlUtils.InteropGetFirstChild(doc);
                if (firstChild != null && (InMetaXmlUtils.InteropGetNodeType(firstChild) == 7 && InMetaXmlUtils.InteropGetNodeName(firstChild) == "xml"))
                {
                    InMetaXmlUtils.InteropRemoveChild(doc, firstChild);
                }
                object processingInstruction = InMetaXmlUtils.InteropCreateProcessingInstruction(doc, "xml", data);
                InMetaXmlUtils.InteropInsertBefore(doc, processingInstruction, InMetaXmlUtils.InteropGetFirstChild(doc));
            }
        }
        public object GetSubNode(object parentElement, string childElementName)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(parentElement);

            if (xmlNode != null)
            {
                return((object)new MsXmlNodeEmulator(xmlNode.SelectSingleNode(childElementName) ?? throw new Exception("Не найден обязательный дочерний элемент \"" + childElementName + "\".")));
            }
            return(InMetaXmlUtils.InteropSelectSingleNode(parentElement, childElementName) ?? throw new Exception("Не найден обязательный дочерний элемент \"" + childElementName + "\"."));
        }
        public string NeedAttr(object element, string attributeName)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(element);

            if (xmlNode != null)
            {
                return(((XmlElement)xmlNode).GetAttribute(attributeName));
            }
            return(InMetaXmlUtils.InteropGetAttribute(element, attributeName) ?? throw new Exception("Элемент не содержит обязательный атрибут \"" + attributeName + "\"."));
        }
        internal static object SelectSingleNode(object node, string pattern)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(node);

            if (xmlNode == null)
            {
                return(InMetaXmlUtils.InteropSelectSingleNode(node, pattern));
            }
            XmlNode node1 = xmlNode.SelectSingleNode(pattern);

            return(node1 != null ? (object)new MsXmlNodeEmulator(node1) : (object)(MsXmlNodeEmulator)null);
        }
        public string GetChildText(object parentElement, string childElementName)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(parentElement);

            if (xmlNode != null)
            {
                XmlNode node = xmlNode.SelectSingleNode(childElementName);
                return(node != null?XmlUtils.GetOwnText(node) : (string)null);
            }
            object element = InMetaXmlUtils.InteropSelectSingleNode(parentElement, childElementName);

            return(element != null?this.GetNodeText(element) : (string)null);
        }
        public object AddElement(
            object parentElement,
            string childElementName,
            string childElementText             = "",
            bool childElementTextAsCDataSection = false)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(parentElement);

            if (xmlNode != null)
            {
                return((object)new MsXmlNodeEmulator((XmlNode)XmlUtils.AppendElement(xmlNode, childElementName, childElementText)));
            }
            object ownerDocument = InMetaXmlUtils.InteropGetOwnerDocument(parentElement);
            object element       = InMetaXmlUtils.InteropCreateElement(ownerDocument, childElementName);

            InMetaXmlUtils.InteropAppendChild(parentElement, element);
            if (!string.IsNullOrEmpty(childElementText))
            {
                InMetaXmlUtils.InteropAppendChild(element, InMetaXmlUtils.InteropCreateTextNode(ownerDocument, childElementText));
            }
            return(element);
        }
        public string GetNodeText(object element)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(element);

            if (xmlNode != null)
            {
                return(XmlUtils.GetOwnText(xmlNode));
            }
            object        nodes         = InMetaXmlUtils.InteropSelectNodes(element, "text()");
            StringBuilder stringBuilder = new StringBuilder();
            int           length        = InMetaXmlUtils.InteropGetLength(nodes);

            for (int index = 0; index < length; ++index)
            {
                object node = InMetaXmlUtils.InteropGetItem(nodes, index);
                stringBuilder.Append(InMetaXmlUtils.InteropGetNodeValue(node));
            }
            stringBuilder.Replace("\r\n", "\r");
            stringBuilder.Replace('\n', '\r');
            stringBuilder.Replace("\r", "\r\n");
            return(stringBuilder.ToString());
        }
        public void SetNodeText(object element, string text, bool textAsCDataSection = false)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(element);

            if (xmlNode != null)
            {
                XmlUtils.ReplaceOwnText(xmlNode, text);
            }
            else
            {
                object nodes  = InMetaXmlUtils.InteropSelectNodes(element, "text()");
                int    length = InMetaXmlUtils.InteropGetLength(nodes);
                for (int index = 0; index < length; ++index)
                {
                    InMetaXmlUtils.InteropRemoveChild(element, InMetaXmlUtils.InteropGetItem(nodes, index));
                }
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }
                InMetaXmlUtils.InteropAppendChild(element, InMetaXmlUtils.InteropCreateTextNode(InMetaXmlUtils.InteropGetOwnerDocument(element), text));
            }
        }
        public string GetAttr(object element, string attributeName, string attributeDefaultValue = "")
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(element);

            return(xmlNode != null?XmlUtils.GetAttr(xmlNode, attributeName, attributeDefaultValue) : InMetaXmlUtils.InteropGetAttribute(element, attributeName) ?? attributeDefaultValue);
        }
        public bool HasAttr(object element, string attributeName)
        {
            XmlNode xmlNode = InMetaXmlUtils.TryGetXmlNode(element);

            return(xmlNode != null ? ((XmlElement)xmlNode).HasAttribute(attributeName) : InMetaXmlUtils.InteropGetAttribute(element, attributeName) != null);
        }
 public void SetAttr(object element, string attributeName, string attributeValue)
 {
     ((XmlElement)InMetaXmlUtils.TryGetXmlNode(element))?.SetAttribute(attributeName, attributeValue);
     InMetaXmlUtils.InteropSetAttribute(element, attributeName, (object)attributeValue);
 }