关闭

对XML的创建/修改/读取/删除

361人阅读 评论(0) 收藏 举报

public void CreateXml(string Path)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null);
            xmldec.Encoding = "gb2312";
            xmldec.Standalone = "yes";
            xmldoc.AppendChild(xmldec);

            XmlElement xmlele = xmldoc.CreateElement("root");
            xmldoc.AppendChild(xmlele);

            xmldoc.Save(Path);
        }

        public void AddXml(string Path,string xmlnode,string NO,string name,string url)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Path);

            XmlNode root = xmldoc.SelectSingleNode("root");
            XmlElement kind2 = xmldoc.CreateElement(xmlnode);
            kind2.SetAttribute("NO", NO);
            kind2.SetAttribute("Name", name);
            kind2.SetAttribute("Url", url);
            root.AppendChild(kind2);

            xmldoc.Save(Path);
        }

        public void DelXml(string Path, string xmlnode)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Path);

            XmlNode root = xmldoc.SelectSingleNode("root");
            XmlNode kind2 = root.SelectSingleNode(xmlnode);
            root.RemoveChild(kind2);

            xmldoc.Save(Path);
        }

        public void ModiXml(string Path, string xmlnode, string NO, string name, string url)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Path);

            XmlNode root = xmldoc.SelectSingleNode("root");
            XmlNode kind2 = root.SelectSingleNode(xmlnode);

            kind2.Attributes["NO"].Value = NO;
            kind2.Attributes["Name"].Value = name;
            kind2.Attributes["Url"].Value = url;

            xmldoc.Save(Path);
        }

        public string[] GetXml(string Path)
        {
            string[] str = new string[4];
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Path);

            XmlNode root = xmldoc.SelectSingleNode("root");
            XmlNode kind2 = root.LastChild;
            str[0] = kind2.Attributes["NO"].Value.ToString();
            str[1] = kind2.Attributes["Name"].Value.ToString();
            str[2] = kind2.Attributes["Url"].Value.ToString();
            str[3] = kind2.Name.ToString();

            return str;
        } 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5743次
    • 积分:151
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档