xml操作类 c#

前端之家收集整理的这篇文章主要介绍了xml操作类 c#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. XML绑定TreeView
  2. privatevoidXmlOperation_Load(objectsender,EventArgse)
  3. {
  4. path=AppDomain.CurrentDomain.BaseDirectory+@"NameList.xml";
  5. xml.Load(path);//加载xml文件
  6. bindTvXml();
  7. }
  8. ///<summary>
  9. ///绑定TreeView
  10. ///</summary>
  11. privatevoidbindTvXml()
  12. {
  13. for(inti=0;i<xml.DocumentElement.ChildNodes.Count;i++)
  14. {
  15. XmlNodeXnode=xml.DocumentElement.ChildNodes[i];
  16. TreeNodenode=newTreeNode();
  17. node.Text=Xnode.Attributes["name"].Value;
  18. node.Tag=Xnode;
  19. bindChildNode(node,Xnode);//绑定子节点
  20. TvXml.Nodes.Add(node);
  21. TvXml.HideSelection=false;
  22. }
  23. }
  24. ///<summary>
  25. ///递归绑定子节点
  26. ///</summary>
  27. ///<paramname="node"></param>
  28. ///<paramname="xml"></param>
  29. privatevoidbindChildNode(TreeNodenode,XmlNodexml)
  30. {
  31. for(inti=0;i<xml.ChildNodes.Count;i++)
  32. {
  33. TreeNodeChildnode=newTreeNode();
  34. XmlNodeChildXml=xml.ChildNodes[i];
  35. Childnode.Text=ChildXml.Value;
  36. Childnode.Name="1";
  37. Childnode.Tag=xml.ChildNodes[i];
  38. if(ChildXml.HasChildNodes)
  39. {
  40. if(ChildXml.ChildNodes[0].NodeType==XmlNodeType.Text)
  41. Childnode.Text=ChildXml.ChildNodes[0].InnerText;
  42. else
  43. bindChildNode(Childnode,ChildXml);
  44. }
  45. node.Nodes.Add(Childnode);
  46. }
  47. }
  48. publicclassManageXML
  49. {
  50. ///<summary>
  51. ///返回用户操作的sql语句
  52. ///</summary>
  53. ///<paramname="sqlNodeName">XML的操作节点</param>
  54. ///<returns>操作的sql语句</returns>
  55. publicstaticstringGetXMLPath(stringstrXMlFileName)
  56. {
  57. stringm_strFullPath="";
  58. AssemblyAsm=Assembly.GetExecutingAssembly();
  59. //获取文件的路径
  60. //m_strFullPath=Asm.Location.Substring(0,(Asm.Location.LastIndexOf("\\")+1))+"XMLsql.xml";
  61. m_strFullPath=AppDomain.CurrentDomain.BaseDirectory+"XMLLibrary\\"+strXMlFileName;
  62. returnm_strFullPath;
  63. }
  64. ///<summary>
  65. ///获取XML数据库中的数据的方法
  66. ///</summary>
  67. ///<paramname="strFilePath">传入文件路径</param>
  68. ///<returns>返回一个数据集</returns>
  69. publicstaticDataSetGetAllDataFromXML(stringstrFilePath)
  70. {
  71. DataSetds=newDataSet();
  72. FileInfofileInfo=newFileInfo(strFilePath);
  73. if(fileInfo.Exists)
  74. {
  75. try
  76. {
  77. ds.ReadXml(strFilePath);
  78. }
  79. catch{}
  80. }
  81. else
  82. {
  83. ds=null;
  84. }
  85. if(ds!=null)
  86. {
  87. if(ds.Tables[0].Rows.Count<1)
  88. ds=null;
  89. }
  90. returnds;
  91. }
  92. ///<summary>
  93. ///获取指定目录下所有子节点的值
  94. ///</summary>
  95. ///<paramname="strFileName">文件路径</param>
  96. ///<paramname="nodeDir">节点目录</param>
  97. ///<returns></returns>
  98. publicstaticHashtableGetNodeList(stringstrFileName,stringnodeDir)
  99. {
  100. HashtablestrNodeList=newHashtable();
  101. try
  102. {
  103. XmlDocumentxmlDoc=newXmlDocument();
  104. xmlDoc.Load(strFileName);
  105. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  106. foreach(XmlNodexninnodeList)//遍历所有子节点
  107. {
  108. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  109. strNodeList.Add(xe.GetAttribute("id").ToString(),xe.InnerText.Trim());
  110. }
  111. }
  112. catch(Exception)
  113. {
  114. throw;
  115. }
  116. returnstrNodeList;
  117. }
  118. ///<summary>
  119. ///获取指定节点的值
  120. ///</summary>
  121. ///<paramname="strFileName">文件路径</param>
  122. ///<paramname="nodeName">节点名称</param>
  123. ///<paramname="value">设置后的值</param>
  124. ///<paramname="nodeDir">指定节点所在的节点目录</param>
  125. ///<returns></returns>
  126. publicstaticstringGetNodeValue(stringstrFileName,stringnodeName,stringnodeDir)
  127. {
  128. stringvalue=null;
  129. try
  130. {
  131. XmlDocumentxmlDoc=newXmlDocument();
  132. xmlDoc.Load(strFileName);
  133. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  134. foreach(XmlNodexninnodeList)//遍历所有子节点
  135. {
  136. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  137. if(xe.Name==nodeName)
  138. {
  139. value=xe.InnerText.Trim();
  140. break;
  141. }
  142. }
  143. }
  144. catch(Exceptionexp)
  145. {
  146. throwexp;
  147. }
  148. returnvalue;
  149. }
  150. ///<summary>
  151. ///获取指定节点下面对应属性的值
  152. ///</summary>
  153. ///<paramname="strFileName">文件路径</param>
  154. ///<paramname="nodeName">节点名称</param>
  155. ///<paramname="nodeDir">指定节点所在的节点目录</param>
  156. ///<paramname="attribute">节点对应的属性名称</param>
  157. ///<returns></returns>
  158. publicstaticstringGetNodeValue(stringstrFileName,stringnodeDir,stringattribute)
  159. {
  160. stringvalue=null;
  161. try
  162. {
  163. XmlDocumentxmlDoc=newXmlDocument();
  164. xmlDoc.Load(strFileName);
  165. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  166. foreach(XmlNodexninnodeList)//遍历所有子节点
  167. {
  168. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  169. if(xe.Name==nodeName)
  170. {
  171. //value=xe.InnerText.Trim();
  172. value=(xe).Attributes[attribute].Value;
  173. break;
  174. }
  175. }
  176. }
  177. catch(Exceptionexp)
  178. {
  179. throwexp;
  180. }
  181. returnvalue;
  182. }
  183. ///<summary>
  184. ///修改指定结点值
  185. ///</summary>
  186. ///<paramname="strFileName">文件路径</param>
  187. ///<paramname="nodeName">节点名称</param>
  188. ///<paramname="value">设置后的值</param>
  189. ///<paramname="nodeDir">指定节点所在的节点目录</param>
  190. ///<returns></returns>
  191. publicstaticboolUpdateNoteValue(stringstrFileName,stringvalue,stringnodeDir)
  192. {
  193. boolisSucceed=false;
  194. try
  195. {
  196. XmlDocumentxmlDoc=newXmlDocument();
  197. xmlDoc.Load(strFileName);
  198. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  199. foreach(XmlNodexninnodeList)//遍历所有子节点
  200. {
  201. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  202. if(xe.Name==nodeName)
  203. {
  204. xe.InnerText=value;
  205. isSucceed=true;
  206. break;
  207. }
  208. }
  209. xmlDoc.Save(strFileName);
  210. }
  211. catch(Exceptionexp)
  212. {
  213. throwexp;
  214. }
  215. returnisSucceed;
  216. }
  217. ///<summary>
  218. ///修改指定结点值
  219. ///</summary>
  220. ///<paramname="strFileName">文件路径</param>
  221. ///<paramname="nodeName">节点名称</param>
  222. ///<paramname="value">设置后的值</param>
  223. ///<paramname="nodeDir">指定节点所在的节点目录</param>
  224. ///<returns></returns>
  225. publicstaticboolUpdateNoteValue(stringstrFileName,stringattribute,stringattributeValue)
  226. {
  227. boolisSucceed=false;
  228. try
  229. {
  230. XmlDocumentxmlDoc=newXmlDocument();
  231. xmlDoc.Load(strFileName);
  232. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  233. foreach(XmlNodexninnodeList)//遍历所有子节点
  234. {
  235. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  236. if(xe.Name==nodeName)
  237. {
  238. xe.InnerText=value;
  239. (xe).Attributes[attribute].Value=attributeValue;
  240. isSucceed=true;
  241. break;
  242. }
  243. }
  244. xmlDoc.Save(strFileName);
  245. }
  246. catch(Exceptionexp)
  247. {
  248. throwexp;
  249. }
  250. returnisSucceed;
  251. }
  252. ///<summary>
  253. ///修改指定结点值
  254. ///</summary>
  255. ///<paramname="strFileName">文件路径</param>
  256. ///<paramname="nodeName">节点名称</param>
  257. ///<paramname="value">设置后的值</param>
  258. ///<paramname="nodeDir">指定节点所在的节点目录</param>
  259. ///<returns></returns>
  260. publicstaticboolUpdateNoteValue(stringstrFileName,Hashtablehstable,stringnodeDir)
  261. {
  262. boolisSucceed=false;
  263. try
  264. {
  265. XmlDocumentxmlDoc=newXmlDocument();
  266. xmlDoc.Load(strFileName);
  267. XmlNodeListnodeList=xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
  268. foreach(DictionaryEntryiteminhstable)
  269. {
  270. foreach(XmlNodexninnodeList)//遍历所有子节点
  271. {
  272. XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
  273. if(xe.Name==item.Key.ToString())
  274. {
  275. xe.InnerText=item.Value.ToString();
  276. isSucceed=true;
  277. break;
  278. }
  279. }
  280. }
  281. xmlDoc.Save(strFileName);
  282. }
  283. catch(Exceptionexp)
  284. {
  285. throwexp;
  286. }
  287. returnisSucceed;
  288. }
  289. }
  1. ///<summary>
  2. ///移除选定节点集的所有属性
  3. ///</summary>
  4. ///<paramname="xmlPathNode"></param>
  5. publicvoidRemoveAllAttribute(stringxmlPathNode)
  6. {
  7. XmlNodeListxnl=objXmlDoc.SelectNodes(xmlPathNode);
  8. foreach(XmlNodexninxnl)
  9. {
  10. xn.Attributes.RemoveAll();
  11. }
  12. }
  13. publicvoidDeleteNode(stringNode)
  14. {
  15. //刪除一个节点。
  16. try
  17. {
  18. stringmainNode=Node.Substring(0,Node.LastIndexOf("/"));
  19. objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
  20. }
  21. catch
  22. {
  23. //throw;
  24. return;
  25. }
  26. }
  27. publicvoidInsertNodeWithChild(stringmainNode,stringChildNode,stringElement,stringContent)
  28. {
  29. //插入一节点和此节点的一子节点。
  30. XmlNodeobjRootNode=objXmlDoc.SelectSingleNode(mainNode);
  31. XmlElementobjChildNode=objXmlDoc.CreateElement(ChildNode);
  32. objRootNode.AppendChild(objChildNode);//插入节点
  33. XmlElementobjElement=objXmlDoc.CreateElement(Element);
  34. objElement.InnerText=Content;
  35. objChildNode.AppendChild(objElement);//插入子节点
  36. }
  37. ///<summary>
  38. ///插入一个节点,带一个Attribute和innerText
  39. ///</summary>
  40. ///<paramname="mainNode"></param>
  41. ///<paramname="Element">节点名称</param>
  42. ///<paramname="Attrib">Attribute名称</param>
  43. ///<paramname="AttribContent">Attribute值</param>
  44. ///<paramname="Content">innerText值</param>
  45. publicvoidInsertNode(stringmainNode,stringAttrib,stringAttribContent,stringContent)
  46. {
  47. XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
  48. XmlElementobjElement=objXmlDoc.CreateElement(Element);
  49. objElement.SetAttribute(Attrib,AttribContent);
  50. objElement.InnerText=Content;
  51. objNode.AppendChild(objElement);
  52. }
  53. ///<summary>
  54. ///插入一个节点,带一个Attribute
  55. ///</summary>
  56. ///<paramname="mainNode"></param>
  57. ///<paramname="Element">节点名称</param>
  58. ///<paramname="Attrib">Attribute名称</param>
  59. ///<paramname="AttribContent">Attribute值</param>
  60. publicvoidInsertNode(stringmainNode,stringAttribContent)
  61. {
  62. XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
  63. XmlElementobjElement=objXmlDoc.CreateElement(Element);
  64. objElement.SetAttribute(Attrib,AttribContent);
  65. objNode.AppendChild(objElement);
  66. }
  67. ///<summary>
  68. ///插入一个节点
  69. ///</summary>
  70. ///<paramname="mainNode"></param>
  71. ///<paramname="Element">节点名称</param>
  72. publicvoidInsertNode(stringmainNode,stringElement)
  73. {
  74. XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
  75. XmlElementobjElement=objXmlDoc.CreateElement(Element);
  76. objNode.AppendChild(objElement);
  77. }
  78. //<summary>
  79. //插入一个节点,带多个属性和一个innertext
  80. //</summary>
  81. publicvoidInsertNode(stringmainNode,stringelementName,string[]arrAttributeName,string[]arrAttributeContent,stringelementContent)
  82. {
  83. try
  84. {
  85. XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
  86. XmlElementobjElement=objXmlDoc.CreateElement(elementName);
  87. for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
  88. {
  89. objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
  90. }
  91. objElement.InnerText=elementContent;
  92. objNode.AppendChild(objElement);
  93. }
  94. catch
  95. {
  96. throw;
  97. //stringt=mainNode;
  98. }
  99. }
  100. ///<summary>
  101. ///插入一个节点,带多个属性
  102. ///</summary>
  103. publicvoidInsertNode(stringmainNode,string[]arrAttributeContent)
  104. {
  105. try
  106. {
  107. XmlNodeobjNode=objXmlDoc.SelectSingleNode(mainNode);
  108. XmlElementobjElement=objXmlDoc.CreateElement(elementName);
  109. for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
  110. {
  111. objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
  112. }
  113. //objElement.InnerText=elementContent;
  114. objNode.AppendChild(objElement);
  115. }
  116. catch
  117. {
  118. throw;
  119. //stringt=mainNode;
  120. }
  121. }
  122. ///<summary>
  123. ///插入子节点(带多个属性)
  124. ///</summary>
  125. ///<paramname="parentNode">要插入的父节点</param>
  126. ///<paramname="elementName">插入的节点名称</param>
  127. ///<paramname="arrAttributeName">属性名称[数组]</param>
  128. ///<paramname="arrAttributeContent">属性内容[数组]</param>
  129. ///<paramname="elementContent">节点内容</param>
  130. publicvoidAddChildNode(stringparentNodePath,stringelementContent)
  131. {
  132. try
  133. {
  134. XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
  135. XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
  136. for(inti=0;i<=arrAttributeName.GetUpperBound(0);i++)
  137. {
  138. objChildElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
  139. }
  140. objChildElement.InnerText=elementContent;
  141. parentNode.AppendChild(objChildElement);
  142. }
  143. catch
  144. {
  145. return;
  146. }
  147. }
  148. ///<summary>
  149. ///插入子节点(将内容以CData形式写入)
  150. ///</summary>
  151. ///<paramname="parentNode">要插入的父节点</param>
  152. ///<paramname="elementName">插入的节点名称</param>
  153. ///<paramname="elementContent">节点内容</param>
  154. publicvoidAddChildNodeCData(stringparentNodePath,stringelementContent)
  155. {
  156. try
  157. {
  158. XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
  159. XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
  160. //写入cData数据
  161. XmlCDataSectionxcds=objXmlDoc.CreateCDataSection(elementContent);
  162. objChildElement.AppendChild(xcds);
  163. parentNode.AppendChild(objChildElement);
  164. }
  165. catch
  166. {
  167. return;
  168. }
  169. }
  170. ///<summary>
  171. ///插入子节点(仅内容,不带属性)
  172. ///</summary>
  173. ///<paramname="parentNode">要插入的父节点</param>
  174. ///<paramname="elementName">插入的节点名称</param>
  175. ///<paramname="elementContent">节点内容</param>
  176. publicvoidAddChildNode(stringparentNodePath,stringelementContent)
  177. {
  178. try
  179. {
  180. XmlNodeparentNode=objXmlDoc.SelectSingleNode(parentNodePath);
  181. XmlElementobjChildElement=objXmlDoc.CreateElement(elementName);
  182. objChildElement.InnerText=elementContent;
  183. parentNode.AppendChild(objChildElement);
  184. }
  185. catch
  186. {
  187. return;
  188. }
  189. }
  190. ///<summary>
  191. ///根据xpath值查找节点
  192. ///</summary>
  193. ///<paramname="NodePath">要查找节点的xpath值</param>
  194. ///<returns>找到返回true,否则返回true</returns>
  195. publicboolFindNode(stringNodePath)
  196. {
  197. try
  198. {
  199. if(objXmlDoc.SelectSingleNode(NodePath)!=null)
  200. {
  201. returntrue;
  202. }
  203. else
  204. {
  205. returnfalse;
  206. }
  207. }
  208. catch
  209. {
  210. returnfalse;
  211. }
  212. }

猜你在找的XML相关文章