using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Xml;namespace PuTianCheng{ /// /// XmlHelper 的摘要说明 /// public class XmlHelper { public XmlHelper() { } /// /// 读取数据 /// /// 路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 /// string /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") ************************************************/ public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 属性名,非空时插入该元素属性值,否则插入元素值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") ************************************************/ public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { } } /// /// 修改数据 /// /// 路径 /// 节点 /// 属性名,非空时修改该节点属性值,否则修改节点值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch { } } /// /// 删除数据 /// /// 路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); doc.Save(path); } catch { } } }}
==================================================
XmlFile.xml:
==================================================
使用方法:
string xml = Server.MapPath("XmlFile.xml");//插入元素//XmlHelper.Insert(xml, "/Root", "Studio", "", "");//插入元素/属性//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "五月软件");//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月软件]", "Master", "", "五月");//插入属性//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月软件]", "", "Url", "http://www.vs2005.com.cn/");//修改元素值//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]/Master", "", "Wuyue");//修改属性值//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]", "Url", "http://www.vs2005.com.cn/");//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]", "Name", "MaySoft");//读取元素值//Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "
名扬银河企业建站源码2.0.2 名扬银河企业建站系统,适用于无代码基础的新手,快速搭建企业网站,程序内置了多项实用功能及插件,能够便捷的对网站进行修改、调整、优化等方面进行操作。【部分功能介绍】1、产品管理发布企业产品信息,管理企业产品,自定义产品封面图,产品详情图、文、视频,产品扩展属性自定义等。2、案例管理系统发布企业成功案例,管理成功案例,自定义案例封面图,案例详情图、文、视频,案例扩展属性自定义等。3、资讯管理系统发布企
0 查看详情
");//读取属性值//Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "
");//读取特定属性值//Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "
");//删除属性//XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");//删除元素//XmlHelper.Delete(xml, "/Root/Studio", "");
以上就是C#-XML操作类的代码实例详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1428905.html
微信扫一扫
支付宝扫一扫