C # An example of the method for reading XML node content

Source: Internet
Author: User

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:

  1. Using system;
  2. Using system. xml;
  3. Using system. xml. XPath;
  4. Using system. DaTa;
  5. Class readxml
  6. {
  7. Public static void main ()
  8. {
  9. String sfile = "readxml. xml ";
  10. // C # Read the XML Node Method 1
  11. Xmldocument Doc = new xmldocument ();
  12. Doc. Load (sfile );
  13. Xmlnode node = Doc. documentelement ["news"] ["content"];
  14. Console. writeline (node. innertext );
  15. // C # Read the XML node method2
  16. Node = Doc. selectsinglenode ("// content ");
  17. Console. writeline (node. innertext );
  18. // Similarly
  19. Node = Doc. documentelement. selectsinglenode ("News/content ");
  20. Console. writeline (node. innertext );
  21. // C # Read the XML Node Method 3
  22. Dataset DS = new dataset ();
  23. DS. readxml (sfile );
  24. Console. writeline (Ds. Tables [0]. Rows [0] ["content"]. tostring ());
  25. // C # Read the XML Node Method 4
  26. Xmltextreader reader = new xmltextreader (sfile );
  27. While (reader. Read ())
  28. {
  29. If (reader. Name = "content ")
  30. {
  31. Console. writeline ("***" + reader. readstring ());
  32. Break;
  33. }
  34. }
  35. Reader. Close ();
  36. // C # Read the XML Node Method 5
  37. Xpathdocument xpdoc = new xpathdocument (sfile );
  38. Xpathnavigator xpnv = xpdoc. createnavigator ();
  39. Xpnv. movetofirstchild ();
  40. Xpnv. movetofirstchild ();
  41. Xpnv. movetofirstchild ();
  42. Xpnv. movetonext ();
  43. Console. writeline ("pathnavigator:" + xpnv. value );
  44. }
  45. }
 
 
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;  } }

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.