Example #1
0
        /// <summary>
        /// 写入节点
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="value">节点的值</param>
        /// <param name="attributes">属性的字典</param>
        public void Write(string nodeName, string value, Dictionary <string, string> attributes)
        {
            XmlNode    node    = XmlDoc.DocumentElement;//得到根节点
            XmlElement element = XmlDoc.CreateElement(nodeName);

            if (attributes != null)
            {
                foreach (KeyValuePair <string, string> pair in attributes)
                {
                    element.SetAttribute(pair.Key, pair.Value);
                }
            }
            element.InnerText = value;
            try
            {
                if (node != null)
                {
                    node.AppendChild(element);
                }
                else
                {
                    XmlDoc.AppendChild(element);
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
            XmlDoc.Save(xmlPath);
        }
Example #2
0
 public XmlDocument GerarXml()
 {
     this.XmlDoc.RemoveAll();
     XmlDoc.PreserveWhitespace = false;
     XmlDoc.AppendChild(XmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
     XmlDoc.AppendChild(Reinf());
     return(XmlDoc);
 }
Example #3
0
        /// <summary>
        /// 添加声明
        /// </summary>
        /// <param name="key">声明类型,比如 xml ,xml-stylesheet</param>
        /// <param name="value">志明值:比如 version="1.0" encoding="utf-8",type="text/xsl" title="XSL Formatting" href="/rss.xsl" media="all"</param>
        public void CreateXmlDeclaration(string key, string value)
        {
            //生成根节点
            XmlNode xn = XmlDoc.CreateNode(XmlNodeType.XmlDeclaration, key, "");

            xn.Value = value;
            XmlDoc.AppendChild(xn);
        }
Example #4
0
        /// <summary>
        /// 在指定的父节点中生成一个子节点
        /// </summary>
        /// <param name="parentNode">父节点(如果为NULL,则此节点为根节点)</param>
        /// <param name="xnType">子节点类型</param>
        /// <param name="name">子节点名称</param>
        /// <returns>返回生成的节点</returns>
        public XmlNode CreateNewNode(XmlNode parentNode, XmlNodeType xnType, string name)
        {
            if (parentNode != null)
            {
                //生成子节点
                XmlNode xn = XmlDoc.CreateNode(xnType, name, "");

                //将子节点添加到父节点下
                parentNode.AppendChild(xn);

                return(xn);
            }
            else
            {
                //生成子节点
                XmlNode xn = XmlDoc.CreateNode(xnType, name, "");

                //添加到根节点
                XmlDoc.AppendChild(xn);

                return(xn);
            }
        }
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="filename">文件名称.xml</param>
        /// <param name="rootnode">根结点</param>
        /// <param name="node">rootnode的子节点</param>
        /// <param name="attributename">rootnode的子节点属性名称</param>
        /// <param name="attributevalue">rootnode的子节点属性值</param>
        /// <param name="subnode">node的子节点</param>
        /// <param name="subatt">node的子节点属性名称</param>
        /// <param name="subattvalue">node的子节点属性名称</param>
        private static string AddNode(string filename, string rootnode, string node, string attributename, string attributevalue, string subnode, string subatt, string subattvalue, object[] prams)
        {
            bool saveorno = false;

            System.Uri u = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

            string xmlpath = System.IO.Path.GetDirectoryName(u.LocalPath) + "\\" + filename;

            if (prams != null && prams.Length > 0)
            {
                if (File.Exists(filename))
                {
                    xmlpath = filename;
                }
            }
            System.Xml.XmlDocument XmlDoc = GetDocument(xmlpath);

            if (XmlDoc == null)
            {
                XmlDoc   = new XmlDocument();
                saveorno = true;
            }
            if (XmlDoc.ChildNodes.Count == 0)
            {
                XmlDeclaration dec = XmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                XmlDoc.AppendChild(dec);
                saveorno = true;
            }

            XmlElement root = (XmlElement)XmlDoc.SelectSingleNode("/" + rootnode);

            if (root == null)
            {
                root = XmlDoc.CreateElement(rootnode);
                XmlDoc.AppendChild(root);
                saveorno = true;
            }
            XmlElement ele = XmlDoc.SelectSingleNode("/" + rootnode + "/" + node) as XmlElement;

            if (ele == null)
            {
                ele = XmlDoc.CreateElement(node);
                if (!string.IsNullOrEmpty(attributename))
                {
                    ele.SetAttribute(attributename, attributevalue);
                }
                if (!string.IsNullOrEmpty(subnode))
                {
                    XmlElement sub = XmlDoc.CreateElement(subnode);
                    if (!string.IsNullOrEmpty(subatt))
                    {
                        sub.SetAttribute(subatt, subattvalue);
                    }
                    ele.AppendChild(sub);
                }
                root.AppendChild(ele);
                saveorno = true;
            }
            else
            {
                XmlNodeList nodes     = XmlDoc.SelectNodes("/" + rootnode + "/" + node);
                XmlNode     existnode = null;
                if (!string.IsNullOrEmpty(attributename))
                {
                    foreach (XmlNode n1 in nodes)
                    {
                        if (n1.Attributes[attributename].Value.Equals(attributevalue))
                        {
                            existnode = n1;
                            break;
                        }
                    }
                    if (existnode == null)
                    {
                        XmlElement ele1 = XmlDoc.CreateElement(node);
                        ele1.SetAttribute(attributename, attributevalue);
                        if (!string.IsNullOrEmpty(subnode))
                        {
                            XmlElement sub = XmlDoc.CreateElement(subnode);
                            ele1.AppendChild(sub);
                        }
                        root.InsertBefore(ele1, ele);
                        saveorno = true;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(subnode))
                        {
                            XmlElement ele2 = (XmlElement)existnode.SelectSingleNode(subnode);
                            if (ele2 == null)
                            {
                                ele2 = XmlDoc.CreateElement(subnode);
                                existnode.AppendChild(ele2);
                                saveorno = true;
                            }
                        }
                    }
                }
                else
                {
                    if (nodes != null)
                    {
                        if (!string.IsNullOrEmpty(subnode))
                        {
                            existnode = nodes[0];
                            XmlElement ele2 = (XmlElement)existnode.SelectSingleNode(subnode);
                            if (ele2 == null)
                            {
                                ele2 = XmlDoc.CreateElement(subnode);
                                existnode.AppendChild(ele2);
                                saveorno = true;
                            }
                        }
                    }
                }
            }
            if (saveorno)
            {
                XmlDoc.Save(xmlpath);
            }
            return(xmlpath);
        }