예제 #1
0
        public string ParseByXpath(string xpath, string aElment)
        {
            XmlNode aNode = this.GetXmlNodeByXpath(xpath);

            return(XmlNodeHelper.ParseByNode(aNode, aElment));
        }
예제 #2
0
        public XmlNode CreateChildXmlNodeWithAttr(XmlNode aNode, string childNodePath, string attrName, string strValue)
        {
            if (aNode != null)
            {
                if (!childNodePath.StartsWith("/"))
                {
                    childNodePath = "/" + childNodePath;
                }

                string searchXpath = string.Format("{0}[@{1}='{2}']", childNodePath, attrName, strValue);

                if (string.IsNullOrEmpty(attrName))
                {
                    searchXpath = childNodePath;
                }

                XmlNode resultNode = XmlNodeHelper.GetXmlNodeByNode(aNode, searchXpath);
                if (resultNode == null)
                {
                    List <string> listPath = new List <string>();
                    string        tmpXpath = childNodePath;

                    while (!string.IsNullOrEmpty(tmpXpath))
                    {
                        listPath.Insert(0, tmpXpath);
                        tmpXpath = tmpXpath.Substring(0, tmpXpath.LastIndexOf("/"));
                    }

                    for (int i = 0; i < listPath.Count; i++)
                    {
                        string  item = listPath[i];
                        XmlNode midNode;
                        if (!string.IsNullOrEmpty(attrName) && i == listPath.Count - 1)
                        {
                            midNode = XmlNodeHelper.GetXmlNodeByNode(aNode, string.Format("{0}[@{1}='{2}']", item, attrName, strValue));
                        }
                        else
                        {
                            midNode = XmlNodeHelper.GetXmlNodeByNode(aNode, item);
                        }

                        if (midNode == null)
                        {
                            string  parentPath = string.Empty;
                            XmlNode parentNode = aNode;
                            if (i > 0)
                            {
                                parentPath = listPath[i - 1];
                                parentNode = XmlNodeHelper.GetXmlNodeByNode(aNode, parentPath);
                            }

                            XmlElement tagChild = this.xmlDoc.CreateElement(item.Substring(parentPath.Length + 1));
                            if (!string.IsNullOrEmpty(attrName) && i == listPath.Count - 1)
                            {
                                XmlAttribute attr = this.xmlDoc.CreateAttribute(attrName);
                                attr.Value = strValue;
                                tagChild.Attributes.Append(attr);
                            }

                            parentNode.AppendChild(tagChild);
                        }
                    }

                    resultNode = XmlNodeHelper.GetXmlNodeByNode(aNode, searchXpath);
                }

                return(resultNode);
            }
            else
            {
                throw new Exception(string.Format("The aNode <{0}> don't exists.Please check.", aNode.Name));
            }
        }