Http://xieycms.blog.163.com/blog/static/281213902009102035425825/
# Read XML node content method example let's take a look at the implementation of C # Read XML nodes:
- Using system;
- Using system. xml;
- Using system. xml. XPath;
- Using system. DaTa;
- Class readxml
- {
- Public static void main ()
- {
- String sfile = "readxml. xml ";
- // C # Read the XML Node Method 1
- Xmldocument Doc = new xmldocument ();
- Doc. Load (sfile );
- Xmlnode node = Doc. documentelement ["news"] ["content"];
- Console. writeline (node. innertext );
- // C # Read the XML node method2
- Node = Doc. selectsinglenode ("// content ");
- Console. writeline (node. innertext );
- // Similarly
- Node = Doc. documentelement. selectsinglenode ("News/content ");
- Console. writeline (node. innertext );
- // C # Read the XML Node Method 3
- Dataset DS = new dataset ();
- DS. readxml (sfile );
- Console. writeline (Ds. Tables [0]. Rows [0] ["content"]. tostring ());
- // C # Read the XML Node Method 4
- Xmltextreader reader = new xmltextreader (sfile );
- While (reader. Read ())
- {
- If (reader. Name = "content ")
- {
- Console. writeline ("***" + reader. readstring ());
- Break;
- }
- }
- Reader. Close ();
- // C # Read the XML Node Method 5
- Xpathdocument xpdoc = new xpathdocument (sfile );
- Xpathnavigator xpnv = xpdoc. createnavigator ();
- Xpnv. movetofirstchild ();
- Xpnv. movetofirstchild ();
- Xpnv. movetofirstchild ();
- Xpnv. movetonext ();
- Console. writeline ("pathnavigator:" + xpnv. value );
- }
- }
My methods
string newQiChar=DateTime.Now .ToString ("yyMMdd"); string phyPath = AppDomain.CurrentDomain.BaseDirectory; string name = "config.xml"; string configPath = Path.Combine(phyPath, name); SaveConfigXml (configPath,newQi,newQiChar);
/// <Summary> /// Save the configuration file /// </Summary> /// <Param name = "savepath"> file path </param> /// <param name = "qishu"> int-type period </param> // <Param name = "qishuchar"> string-type period </param> /// <returns> </ returns> Public static bool saveconfigxml (string savepath, int qishu, string qishuchar) {bool flag = false; try {xmldocument Doc = new xmldocument (); bool exists = system. io. file. exists (savepath); If (! Exists) {// generate Doc. loadxml ("<configs> </configs>"); // create the root node maxqishu xmlnode root = Doc. selectsinglenode ("configs"); xmlelement maxqishu = Doc. createelement ("maxqishu"); maxqishu. setattribute ("ID", "qishu"); maxqishu. setattribute ("value", qishu. tostring (); maxqishu. setattribute ("text", qishuchar); root. appendchild (maxqishu); Doc. save (savepath);} else {// modify Doc. load (savepath); xmlelement Xe = (xmlel Ement) Doc. selectsinglenode ("configs/maxqishu"); If (Xe! = NULL) {Xe. setattribute ("ID", "qishu"); Xe. setattribute ("value", qishu. tostring (); Xe. setattribute ("text", qishuchar); Doc. save (savepath); // save}
} flag = true; } catch (Exception ex) { flag = false; } return flag; }
/// <Summary> /// Number of read periods from the configuration file /// </Summary> /// <Param name = "savepath"> </param> /// <Param name = "type"> Number of 1-int digit types, 0-string type period </param> // <returns> </returns> Public static object readmaxqishufromxml (string savepath, int type) {object result = string. empty; try {xmldocument Doc = new xmldocument (); bool exists = system. io. file. exists (savepath); If (exists) {// Doc. load (savepath); # region/* string xpathpath = ""; if (type = 1) {// Number of numbers xpathpath = "configs/maxqishu/value ";} else {xpathpath = "configs/maxqishu/text";} xmlnode node = (xmlnode) Doc. selectsinglenode (xpathpath); Result = node. innertext; **/# endregion xmlelement Xe = (xmlelement) Doc. selectsinglenode ("configs/maxqishu"); string name = Xe. name; string value = Xe. value; If (type = 1) {result = Xe. getattribute ("value");} else {result = Xe. getattribute ("text ");}}
} catch (Exception ex) { } return result; } }