. NET general operations for XML

Source: Internet
Author: User

For directly creating an XML file this, skip directly. Assume that the initial content of the XML is the following side:

<? XML version= "1.0" ?> < Conf > </ Conf >

Where the root node is CONF.

Adding nodes
 Public Static BOOLaddconfig (Commonconfig conf) {Try{XmlDocument doc=NewXmlDocument (); Doc.                Load (Configpath); XmlElement Root=Doc.                DocumentElement; XmlElement Body= Doc. CreateElement ("Config"); XmlElement commoncontent= Doc. CreateElement ("commoncontent"); Commoncontent.innertext=conf.commoncontent; XmlElement commonpriority= Doc. CreateElement ("commonpriority"); Commonpriority.innertext=conf.CommonPriority.ToString (); Body.                AppendChild (commoncontent); Body.                                AppendChild (commonpriority); XmlAttribute Attr= Doc. CreateAttribute ("CommonName"); Attr.innertext=Conf.commonname; Body.                Attributes.append (ATTR); Root.                AppendChild (body); Doc.                Save (Configpath); return true; }            Catch(Exception ex) {Logmsg.writelog ("General Configuration Add configuration information exception:"+Ex.                ToString ()); return false; }        }        #endregion

Edit Node
 Public Static BOOLmodifyconfig (Commonconfig conf) {Try{XmlDocument doc=NewXmlDocument (); Doc.                Load (Configpath); XmlElement Root=Doc.                DocumentElement; XmlNode Commoncontentnode= root. selectSingleNode ("/conf/config[@CommonName = '"+ Conf.commonname +"']/commoncontent"); Commoncontentnode.innertext=conf.commoncontent; //because the attribute commonname is used to determine the node, the modified action is placed at the end, otherwise the child node will not getXmlNode Confnode = root. selectSingleNode ("/conf/config[@CommonName = '"+ Conf.commonname +"']"); confnode.attributes["CommonName"]. Value =Conf.                Newcommonname; Doc.                Save (Configpath); return true; }            Catch(Exception ex) {Logmsg.writelog ("Common Configuration Modify configuration information exception:"+Ex.                ToString ()); return false; }        }

Get all nodes
 Public StaticList<commonconfig>Getallconfig () {List<CommonConfig> listconf =NewList<commonconfig>(); stringCacheKey ="Commonconfig_cache"; System.Web.Caching.Cache Objcache=Httpruntime.cache; if(Objcache[cachekey] = =NULL) {XmlDocument doc=NewXmlDocument (); Doc.                Load (Configpath); XmlElement Root=Doc.                DocumentElement; XmlNodeList NodeList=Root.                ChildNodes; foreach(XmlNode CurnodeinchnodeList) {Commonconfig conf=NewCommonconfig (); Conf.commonname= curnode.attributes["CommonName"].                    Value; XmlNode Contentnode=curnode.selectsinglenode ("commoncontent"); Conf.commoncontent=Contentnode.innertext; XmlNode Prioritynode= Curnode.selectsinglenode ("commonpriority"); Conf.commonpriority=Convert.ToInt32 (Prioritynode.innertext);                Listconf.add (conf); } System.Web.Caching.CacheDependency DEP=NewSystem.Web.Caching.CacheDependency (Configpath);                Datacache.setcache (CacheKey, listconf, DEP); returnlistconf; }            Else            {                returnObjcache[cachekey] asList<commonconfig>; }        }

The end result is this:

<?XML version= "1.0"?><Conf>  <ConfigCommonName= "website logo">    <commoncontent>Website logo</commoncontent>    <commonpriority>1</commonpriority>  </Config></Conf>

The above need to use the XPath syntax, I also can not remember, so put a link here, later used to go to see on the line.

You might want to add some operations to the XML later ...

. NET general operations for XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.