Recently, I did not have much to do in the office, and I was not able to sit around, so I learned XML. Of course, I learned a little time, so I learned it quite superficial. I personally think, like these things. you can use it. As for the principle and so on, you will feel it.
I think XML is similar to the INI (initialization file) in VC ++. Put some necessary data or data that must be loaded at startup into these text files, because they all have a standard format, It is very convenient to read or insert data into it.
Source code download
Public xmldocument xmldoc; // XML instance public xmlnode; // public xmlelement xmlelem;
Xmldocument is an XML instance. xmlnode is an XML node xmlelement.
/// // First generate. XML file, and insert data into it, in fact, it is very simple, there is a fixed mode, of course there are several ways, here I only list one of the methods, for your reference only
// Obtain the directory of the current project
String stringcurrdir = system. io. directory. getcurrentdirectory (); // get the MessageBox directory where the current executable file is located. show (stringcurrdir); stringcurrdir = stringcurrdir + @ "\ data. XML "; // generate data. XML file
// By the way, why do I use @ before some strings @
@ "\ Data. XML ", you can see that there is an escape character '\' here. If it is not @, an error will be reported. I think people who have some programming experience will be very clear about this. I have no experience, just come across it by chance. I will share it with you here. the following error occurs.
Xmldoc = new xmldocument (); // Add the Declaration section of XML, <? XML version = "1.0" encoding = "gb2312"?> Xmldeclaration xmldecl; xmldecl = xmldoc. createxmldeclaration ("1.0", "gb2312", null); xmldoc. appendchild (xmldecl); // Add a root element xmlelem = xmldoc. createelement ("", "employees", ""); xmldoc. appendchild (xmlelem); // Add another element for (INT I = 1; I <3; I ++) {xmlnode root = xmldoc. selectsinglenode ("employees"); // find <employees> xmlelement xe1 = xmldoc. createelement ("Node"); // create a <node> node xe1.setattribute ("genre", ""); xe1.setattribute ("ISBN ", "2-3631-4"); xmlelement xesub1 = xmldoc. createelement ("title"); xesub1.innertext = "VC ++ from getting started to proficient"; // set the text node xe1.appendchild (xesub1); xmlelement xesub2 = xmldoc. createelement ("author"); xesub2.innertext = ""; // set the text node xe1.appendchild (xesub2); xmlelement xesub3 = xmldoc. createelement ("price"); xesub3.innertext = "53.8"; // set the text node xe1.appendchild (xesub3); root. appendchild (xe1);} // xmldoc. save (system. io. directory. getcurrentdirectory (); xmldoc. save (stringcurrdir );
/// Add a node
Xmldocument xmldoc1 = new xmldocument (); string stringcurrdir = system. io. directory. getcurrentdirectory (); stringcurrdir = stringcurrdir + @ "\ data. XML "; xmldoc1.load (stringcurrdir); xmlnode root = xmldoc1.selectsinglenode (" employees "); // search for <employees> xmlelement xe1 = xmldoc1.createelement (" Node "); // create a <node> node xe1.setattribute ("genre", "zhangsan"); // set the genre attribute xe1.setattribute ("ISBN ", "1-1111-1"); // set the ISBN attribute of the node xmlelement xesub1 = xmldoc1.createelement ("title"); xesub1.innertext = "C # Getting Started help "; // set the text node xe1.appendchild (xesub1); // Add it to the <node> node xmlelement xesub2 = xmldoc1.createelement ("author"); operator = ""; xe1.appendchild (xesub2 ); xmlelement xesub3 = xmldoc1.createelement ("price"); xesub3.innertext = "123.3"; xe1.appendchild (xesub3); root. appendchild (xe1); xmldoc1.save (stringcurrdir );
// Read the data in XML
If (mark! = 0) // mark. When you click the button repeatedly, if The ListBox is already displayed, clear the data in {listbox1.items. clear ();} xmldocument xmldoc1 = new xmldocument (); string stringcurrdir = system. io. directory. getcurrentdirectory (); stringcurrdir = stringcurrdir + @ "\ data. XML "; xmldoc1.load (stringcurrdir); // obtain all child nodes of the employees node xmlnodelist nodelist = xmldoc1.selectsinglenode (" employees "). childnodes; foreach (xmlnode Xn in nodelist) // traverses all subnodes {xmlelement Xe = (xmlelement) xn; // converts the subnode type to xmlelement type string TT = Xe. getattribute ("genre"); listbox1.items. add (TT); // continue to obtain all the sub-nodes of Xe sub-nodes xmlnodelist NLS = Xe. childnodes; foreach (xmlnode xn1 in NLS) {xmlelement xe2 = (xmlelement) xn1; listbox1.items. add ("" + xe2.name + "" + xe2.innertext) ;}} mark = 1 ;}
Generated XML
Of course, this is what I learned from the resources I found online. The specific link address is as follows. You should also learn it.
Http://www.cnblogs.com/zhaoxinxin/articles/1446174.html
I have just started to write a blog. If you are not writing well, please make a brick. If you have any questions, please discuss them together and comment.