資源描述:
《c#操作xml:增,刪,改,查操作》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、c#操作xmlhttp://blog.myspace.cn/e/404232246.htm同樣是增,刪,改,查四種操作?,F(xiàn)在我們就詳細(xì)介紹一下增和刪兩種操作??聪旅娴囊粋€(gè)xml示例:4565465jjjjj3456789xml的每個(gè)節(jié)點(diǎn)分為節(jié)點(diǎn)名稱(chēng),節(jié)點(diǎn)值和屬性三類(lèi),如:member節(jié)點(diǎn):id為
2、其一個(gè)屬性,menber為其節(jié)點(diǎn)名稱(chēng)。再如:name節(jié)點(diǎn)456為其節(jié)點(diǎn)值。如果我們需要增加一個(gè)節(jié)點(diǎn)可以調(diào)用一下幾個(gè)方法://////在根節(jié)點(diǎn)下添加父節(jié)點(diǎn)///publicstaticvoidAddParentNode(stringxmlPath,stringparentNode){XmlDocumentxdoc=newXmlDocument();xdoc.Load(xmlPath);//創(chuàng)建一個(gè)新的menber節(jié)點(diǎn)并將它添加到根節(jié)點(diǎn)下XmlElementNode=xdoc.CreateElement(parentNode);xdoc.Document
3、Element.PrependChild(Node);xdoc.Save(xmlPath);}#region插入一節(jié)點(diǎn),帶一屬性//////插入一節(jié)點(diǎn),帶一屬性//////Xml文檔路徑///當(dāng)前節(jié)點(diǎn)路徑///新節(jié)點(diǎn)///屬性名稱(chēng)///屬性值///4、ramname="Content">新節(jié)點(diǎn)值publicstaticvoidXmlInsertElement(stringxmlPath,stringMainNode,stringElement,stringAttrib,stringAttribContent,stringContent){XmlDocumentobjXmlDoc=newXmlDocument();objXmlDoc.Load(xmlPath);XmlNodeobjNode=objXmlDoc.SelectSingleNode(MainNode);XmlElementobjElement=objXmlDo
5、c.CreateElement(Element);objElement.SetAttribute(Attrib,AttribContent);objElement.InnerText=Content;objNode.AppendChild(objElement);objXmlDoc.Save(xmlPath);}#endregion#region插入一節(jié)點(diǎn)不帶屬性publicstaticvoidXmlInsertElement(stringxmlPath,stringMainNode,stringElement,stringContent){XmlDocumentobjXmlDoc=ne
6、wXmlDocument();objXmlDoc.Load(xmlPath);XmlNodeobjNode=objXmlDoc.SelectSingleNode(MainNode);XmlElementobjElement=objXmlDoc.CreateElement(Element);objElement.InnerText=Content;objNode.AppendChild(objElement);objXmlDoc.Save(xmlPath);}#endregion#region向一個(gè)節(jié)點(diǎn)添加屬性//////向一個(gè)節(jié)點(diǎn)添加屬性//////
7、xml文件路徑///節(jié)點(diǎn)路徑///要添加的節(jié)點(diǎn)屬性的名稱(chēng)///要添加屬性的值publicstaticvoidAddAttribute(stringxmlPath,str