读、写、查XML函数

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
namespace AccessController
{
    class ClassXMLAssgin
    {
        //const string XMLname = "assign.xml";
        const string FatherNode = "devicepoint";
        const string SubNode = "device";
        string path = null;
        public struct devicepoint
        {
            public int num;
            public string name;
            public Point  p ;
            public devicepoint(int number,string nam,Point pt)
            {
                num = number;
                name = nam;
                p = new Point(0, 0);
                p.X = pt.X;
                p.Y = pt.Y;
            }
        };
        public ClassXMLAssgin()
        {
            path = AppDomain.CurrentDomain.BaseDirectory + @"\XML\assign.xml";
        
        }
        public void writeToAssginXML(int num,string name,Point p)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNode root = xmlDoc.SelectSingleNode(FatherNode);
            XmlElement xe1 = xmlDoc.CreateElement(SubNode);
            xe1.SetAttribute("num", num.ToString());
            xe1.SetAttribute("Name", name);

            XmlElement xesub1 = xmlDoc.CreateElement("position");
            xesub1.SetAttribute("X", p.X.ToString());
            xesub1.SetAttribute("Y", p.Y.ToString());
            xe1.AppendChild(xesub1);//添加到<book>节点中
            
            root.AppendChild(xe1);
            xmlDoc.Save(path);

        }
        public void updateToAssginXML(string name, Point p)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(FatherNode).ChildNodes;
            foreach (XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型    
                if (xe.GetAttribute("Name") == name.ToString())  
                {
                    XmlElement xe1 = (XmlElement)xe;
                    XmlNodeList nls=xe1.ChildNodes;//继续获取xe子节点的所有子节点        
                    foreach (XmlNode xn1 in nls)//遍历         
                    {
                        XmlElement xe2 = (XmlElement)xn1;//转换类型              
                        if (xe2.Name == "position")//如果找到         
                        {
                            xe2.SetAttribute("X", p.X.ToString());
                            xe2.SetAttribute("Y", p.Y.ToString());
                            break;
                        }

                    }
                }
            }
            xmlDoc.Save(path);//保存。
        }
        public void delFromAssginXML(int num)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNode fathernode = xmlDoc.SelectSingleNode(FatherNode);
            XmlNodeList xnl = xmlDoc.SelectSingleNode(FatherNode).ChildNodes;
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("num") == num.ToString())
                {
                    fathernode.RemoveChild(xn);
                    //xe.RemoveAll();//删除该节点的全部内容   
                }
            }
            xmlDoc.Save(path);
        }
        public string FindDeciveNameFromAssginXML(int num)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNode fathernode = xmlDoc.SelectSingleNode(FatherNode);
            XmlNodeList xnl = xmlDoc.SelectSingleNode(FatherNode).ChildNodes;
            string devicename = null;
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("num") == num.ToString())
                {
                   devicename =  xe.GetAttribute("Name");
                    //xe.RemoveAll();//删除该节点的全部内容  
                   break;
                }
            }
            xmlDoc.Save(path);
            return devicename;
        }
        public ArrayList getSetFromAssginXML()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList xnl = xmlDoc.SelectSingleNode(FatherNode).ChildNodes;
            ArrayList nameList = new ArrayList();
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点        
                foreach(XmlNode xn1 in nls)//遍历         
                {              
                    XmlElement xe2=(XmlElement)xn1;//转换类型              
                    if(xe2.Name=="position")//如果找到              
                    {
                        Point pt = new Point(int.Parse(xe2.GetAttribute("X")),int.Parse(xe2.GetAttribute("Y")));
                        devicepoint devicepoint = new ClassXMLAssgin.devicepoint(int.Parse(xe.GetAttribute("num")), xe.GetAttribute("Name"), pt);
                        nameList.Add(devicepoint);                
                        break;//找到就退出来           
                    }        
                }    
                
            }
            xmlDoc.Save(path);
            return nameList;
        }
    }
}

 

posted on 2013-11-15 17:36  筑梦1582  阅读(201)  评论(0编辑  收藏  举报