C# 处理xml

今天下午闲来无事,看来一下C#是怎么处理xml的。看起来还是蛮简单的,随手写了几行代码。

我是用了XmlDocument

 

创建xml

基本上,可以一步一步来构建一个xml文件,如下:

XmlDocument doc = new XmlDocument();

            bool bExists = true;
            try
            {
                doc.Load(XMLPath);
            }
            catch (System.Exception ex)
            {
                bExists = false;
            }

            XmlNode xmlnode;
            if (!bExists)
            {
                xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                doc.AppendChild(xmlnode);

                xmlnode = doc.CreateElement("Root");
                doc.AppendChild(xmlnode);
            }
            else
            {
                xmlnode = doc.SelectSingleNode("Root");
            }

            //构造一个student信息node
            XmlElement s = doc.CreateElement("Student");
            xmlnode.AppendChild(s);
            XmlElement name = doc.CreateElement("Name");
            XmlElement age = doc.CreateElement("Age");
            XmlElement address = doc.CreateElement("Address");
            s.AppendChild(name);
            s.AppendChild(age);
            s.AppendChild(address);

            XmlNode content = doc.CreateTextNode(Name);
            name.AppendChild(content);
            content = doc.CreateTextNode(string.Format("{0}", Age));
            age.AppendChild(content);
            content = doc.CreateTextNode(Address);
            address.AppendChild(content);

            try
            {
                doc.Save(XMLPath);
            }
            catch (System.Exception ex)
            {
            	
            }


 

删除或者修改xml

 XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(XMLPath);
            }
            catch (System.Exception ex)
            {
                return;
            }

            XmlNode root = doc.SelectSingleNode("Root");
            if (root == null)
            {
                return;
            }

            XmlNodeList childs = root.ChildNodes;

            foreach (XmlNode node in childs)
            {
                Student s = new Student();

                XmlNode content = node.SelectSingleNode("Name");
                s.Name = content.InnerText;
                content = node.SelectSingleNode("Age");
                s.Age = System.Int32.Parse(content.InnerText);
                content = node.SelectSingleNode("Address");
                s.Address = content.InnerText;

                if (s.Name == this.Name)
                {
                    if (bRemove)
                    {
                        root.RemoveChild(node);//直接删除节点
                    }
                    else
                    {//修改修改节点的内容
                        node.SelectSingleNode("Name").InnerText = this.Name;
                        node.SelectSingleNode("Age").InnerText = string.Format("{0}", this.Age);
                        node.SelectSingleNode("Address").InnerText = this.Address;
                    }
                    
                }
            }

            doc.Save(XMLPath);


 随手写了个例子,仅供参考。

http://download.csdn.net/detail/zj510/5099776

 

你可能感兴趣的:(xml,C#)