插入xml文档操作类c

c xml  时间:2021-02-14  阅读:()

xml文档操作C#objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration( 1.0,utf-8 ,null));//置xml的版本格式信息objXmlDoc.AppendChild(objXmlDoc.CreateElement( , sRoot, ));//建根元素objXmlDoc.Save(XmlFile);//保存else//否 文件是否存在 不存在 建if(!(File.Exists(XmlFile)))objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration( 1.0,utf-8,null));objXmlDoc.AppendChild(objXmlDoc.CreateElement( , sRoot, ));objXmlDoc.Save(XmlFile);objXmlDoc.Load(XmlFile);catch(System.Exception ex)throw ex;strXmlFile=XmlFile;

///param name=XmlPathNode xPath /param

///returns有数据返回DataView 否返回null/returnspublic DataView GetData(string XmlPathNode)

//找数据。返回一个D ataVi ew

DataSet ds=new DataSet();try

StringReader read = newStringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);ds.ReadXml(read);

return ds.Tables[0].DefaultView;c atch

//throw;return null;

///summary

///更新点内容

////summary

///param name=xmlPathNode/param

///param name=content/parampublic void UpdateNode(string xmlPathNode, string content)objXmlDoc.SelectSingleNode(xmlPathNode).InnerText=content;

///summary

///更新点的某个属性

////summary

///param name=xmlPathNode要操作的点/param

///p aram name=AttribName属性名/p aram

///param name=AttribValue属性 /parampublic void UpdateNode(string xmlPathNode, string AttribName, string AttribValue)

((XmlElement)(objXmlDoc.SelectSingleNode(xmlPathNode))).SetAttribute(AttribName,AttribValue);

///param name=xmlPathNode要操作点的xpath句/param

///p aram name=arrAttribName属性名称字符串数 /p aram

///p aram name=arrAttrib Content属性内容字符串数 /param

///param name=content 点内容/parampublic void UpdateNode(string xmlPathNode, string[] arrAttribName, string[] arrAttribContent,string content)

XmlNode xn=objXmlDoc.SelectSingleNode(xmlPathNode);if(xn!=null)xn.InnerText=content;xn.Attributes.RemoveAll();for(int i=0; i=arrAttribName.GetUpperB ound(0); i++)

((XmlElement)(xn)).S etAttribute(arrAttribName[i],arrAttrib C ontent[i]);

///summary

///移除定点集的所有属性

////summary

///param name=xmlPathNode/parampublic void RemoveAllAttribute(string xmlPathNode)

XmlNodeList xnl=objXmlDoc.SelectNodes(xmlPathNode);foreach(XmlNode xn in xnl)xn.Attributes.RemoveAll();string mainNode=Node.Substring(0,Node.LastIndexOf(/));objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));c atch

//throw;

r e turn;public void InsertNodeWithChild(string mainNode, string ChildNode, string Element, stringContent)

//插入一点和此点的一子点。

XmlNode objRootNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement obj ChildNode=objXmlDoc.CreateElement(ChildNode);objRootNode.AppendChild(obj ChildNode);//插入点

XmlElement objElement=objXmlDoc.CreateElement(Element);obj Element.InnerText=Content;obj ChildNode.AppendChild(objElement);//插入子点

///summary

///插入一个点 一个Attribute和innerText

////summary

///param name=mainNode/param

///param name=Element 点名称/param

///p aram name=Attrib Attribute名称/param

///param name=Attrib Content Attribute /param

///param name=Content innerText /parampublic void InsertNode(string mainNode, string Element, string Attrib, string AttribContent,string Content)

XmlNode objNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement objElement=objXmlDoc.CreateElement(Element);obj Element.S etAttribute(Attrib,Attrib Content);

obj Element.InnerText=Content;objNode.AppendChild(objElement);

///summary

///插入一个点 一个Attrib ute

////summary

///param name=mainNode/param

///param name=Element 点名称/param

///p aram name=Attrib Attribute名称/param

///param name=Attrib Content Attribute /parampublic void InsertNode(string mainNode, string Element, string Attrib, string AttribContent)XmlNode objNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement objElement=objXmlDoc.CreateElement(Element);obj Element.S etAttribute(Attrib,Attrib Content);objNode.AppendChild(objElement);

///param name=Element 点名称/parampublic void InsertNode(string mainNode, string Element)

XmlNode objNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement objElement=objXmlDoc.CreateElement(Element);objNode.AppendChild(objElement);

///summarypublic void InsertNode(string mainNode, string elementName, string[] arrAttributeName,

string[]arrAttributeContent, string elementContent)try

XmlNode objNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement objElement=objXmlDoc.CreateElement(elementName);for(int i=0; i=arrAttributeName.GetUpperB ound(0); i++)obj Element.S etAttribute(arrAttributeName[i],arrAttribute C ontent[i]);obj Element.InnerText=elementContent;objNode.AppendChild(objElement);c atchthrow;

//string t=mainNode;

//;

///summary

///插入一个点 多个属性

////summarypublic void InsertNode(string mainNode, string elementName, string[] arrAttributeName,string[]arrAttribute Content)try

XmlNode objNode=objXmlDoc.SelectSingleNode(mainNode);

XmlElement objElement=objXmlDoc.CreateElement(elementName);for(int i=0; i=arrAttributeName.GetUpperB ound(0); i++)obj Element.S etAttribute(arrAttributeName[i],arrAttribute C ontent[i]);

//obj Element.InnerText=elementContent;

objNode.AppendChild(objElement);c atchthrow;

//string t=mainNode;

//;

///summary

///插入子点(多个属性)

////summary

///param name=parentNode要插入的父点/param

///param name=elementName插入的点名称/param

///p aram name=arrAttributeName属性名称[数] /p aram

///p aram name=arrAttribute Content属性内容[数] /param

///param name=elementContent 点内容/parampublic void AddChildNode(string parentNodePath, string elementName, string[]arrAttributeName, string[]arrAttributeContent, string elementContent)try

XmlNode parentNode=objXmlDoc.SelectSingleNode(parentNodePath);

XmlElement obj ChildElement=objXmlDoc.CreateElement(elementName);for(int i=0; i=arrAttributeName.GetUpperB ound(0); i++)obj ChildElement.S etAttribute(arrAttributeName[i],arrAttribute Content[i]);obj ChildElement.InnerText=elementContent;parentNode.AppendChild(obj ChildElement);

c atchr e turn;

///summary

///插入子点(将内容以CD ata形式写入)

////summary

///param name=parentNode要插入的父点/param

///param name=elementName插入的点名称/param

///param name=elementContent 点内容/parampublic void AddChildNodeCData(string parentNodePath, string elementName, stringelementContent)try

XmlNode parentNode=objXmlDoc.SelectSingleNode(parentNodePath);

XmlElement obj ChildElement=objXmlDoc.CreateElement(elementName);

//写入cData数据

XmlCDataSection xcds=objXmlDoc.CreateCDataSection(elementContent);obj ChildElement.AppendChild(xcds);parentNode.AppendChild(obj ChildElement);c atchr e turn;

///param name=parentNode要插入的父点/param

///param name=elementName插入的点名称/param

///param name=elementContent 点内容/param

public void AddChildNode(string parentNodePath, string elementName, string elementContent)try

XmlNode parentNode=objXmlDoc.SelectSingleNode(parentNodePath);

XmlElement obj ChildElement=objXmlDoc.CreateElement(elementName);obj ChildElement.InnerText=elementContent;parentNode.AppendChild(obj ChildElement);c atchr e turn;

///summary

///根据xp ath 找点

////summary

///p aram name=NodeP ath要找点的xp ath /p aram

///returns找到返回true,否返回true/returnspublic bool FindNode(string NodePath)tryif(objXmlDoc.SelectSingleNode(NodePath) !=null)return true;elsereturn false;c atchreturn false;

用方法

BlueHost主机商年中618活动全场低至五折

BlueHost 主机商在以前做外贸网站的时候还是经常会用到的,想必那时候有做外贸网站或者是选择海外主机的时候还是较多会用BlueHost主机商的。只不过这些年云服务器流行且性价比较高,于是大家可选择商家变多,但是BlueHost在外贸主机用户群中可选的还是比较多的。这次年中618活动大促来袭,毕竟BLUEHOST商家目前中文公司设立在上海,等后面有机会也过去看看。他们也会根据我们的国内年中促销发...

妮妮云80元/月,香港站群云服务器 1核1G

妮妮云的来历妮妮云是 789 陈总 张总 三方共同投资建立的网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑妮妮云的市场定位妮妮云主要代理市场稳定速度的云服务器产品,避免新手购买云服务器的时候众多商家不知道如何选择,妮妮云就帮你选择好了产品,无需承担购买风险,不用担心出现被跑路 被诈骗的情况。妮妮云的售后保证妮妮云退款 通过于合作商的友好协商,云服务器提供2天内全额退款,超过2天不退款 物...

飞讯云E5-2678V3 64GB,湖北十堰100G高防物理机330元/月

飞讯云官网“飞讯云”是湖北飞讯网络有限公司旗下的云计算服务品牌,专注为个人开发者用户、中小型、大型企业用户提供一站式核心网络云端部署服务,促使用户云端部署化简为零,轻松快捷运用云计算。飞讯云是国内为数不多具有ISP/IDC双资质的专业云计算服务商,同时持有系统软件著作权证书、CNNIC地址分配联盟成员证书,通过了ISO27001信息安全管理体系国际认证、ISO9001质量保证体系国际认证。 《中华...

c xml为你推荐
961556225317563152822是哪个银行的flash导航条谁来帮我看看这样的flash导航条 下面的页面该怎么设计吴晓波频道买粉罗辑思维,晓松奇谈,鸿观,吴晓波频道,财经郎眼哪个更有深度依赖注入依赖注入到底是为了解决什么问题的1433端口如何打开1433端口网站联盟网络联盟是什么意思申请证书申请毕业证书申请证书求高手教下怎么申请证书办公协同软件协同企业办公的软件有哪些?奇虎论坛奇虎是中国的吗?
北京vps 分销主机 wavecom idc评测网 正版win8.1升级win10 realvnc 好看的桌面背景大图 500m空间 速度云 cn3 华为云盘 宏讯 英国伦敦 阿里云免费邮箱 国外网页代理 免费稳定空间 SmartAXMT800 此网页包含的内容将不使用安全的https ftp是什么东西 神棍节 更多