How to operate XML in C # .NET

xiaoxiao2021-04-06  293

How to operate XML in C # .NET requires the namespace to add: use system.xml;

Define several public objects: xmldocument Xmldoc; XMLNode XMLNode; XMLELEMENT XMLEM;

1. Create an XML file to the server's same name directory:

Method 1: Xmldoc = new xmldocument (); // Add XML declaration paragraph, Xmldeclaration Xmldecl; xmldecl = xmldoc.createxmldeclaration ("1.0", "GB2312", NULL); xmldoc.appendchild (xmldecl); // Add a root element XMLELEM = xmldoc.createElement ("" "" ",", "); xmldoc.Appendchild (xmlelem); // Add another element for (INT i = 1; i <3; i ) {

XMLNode root = xmldoc.selectsinglenode ("Employees"); // Find Xmlelement Xe1 = XmLDoc.createElement ("Node"); // Create a Node Xe1.Setattribute ("Genre", "Li Zan Hong") ; // Set the node GENRE attribute XE1.SetaTribute ("ISBN", "2-3631-4"); // Setting the node ISBN property

XMLELEMENT XESUB1 = XMLDoc.createElement ("Title"); Xesub1.innertext = "CS From Getting Started to Wen"; // Settings Text Node XE1.Appendchild (Xesub1); // Add to Node XMLELEment XESUB2 = XMLDOC. CreateElement ("author"); Xesub2.innertext = "Waiting"; Xe1.Appendchild (Xesub2); XMLELEMENT XESUB3 = XMLDoc.createElement ("price"); XESUB3.INNERTEXT = "58.3"; XE1.Appendchild (Xesub3);

Root.appendchild (xe1); // Add to node} // Save the created XML document XMLDoc.save (Server.MAppath ("DATA.XML"));

/ / The content named Data.xml is generated in the directory of the same name. The content is as follows,

xmlWriter = new XmlTextWriter (strFilename, Encoding.Default); // create an xml document xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument (); xmlWriter.WriteStartElement ( "Employees");

XMLWRITER.WRITESTARTELEMENT ("Node"); XMLWRITER.WRITEATTRIBUTESTRING ("Genre", "Li Zanli"); XMLWriter.writeAttributeString ("ISBN", "2-3631-4");

XMLWRITER.WRITESTARTELEMENT ("Title"); XMLWRITER.WRITESTRING ("CS from getting started to proficiency"); xmlwriter.writeEndelement ();

XMLWRITER.WRITESTARTELEMENT ("Author"); XMLWRITER.WRITESTRING ("Waiting"); xmlwriter.writeEndelement ();

XMLWRITER.WRITESTARTELEMENT ("Price"); XMLWRITER.WRITESTRING ("58.3"); XMLWRITER.WRITEENDEEMENT ();

Xmlwriter.writeEndelement ();

Xmlwriter.close (); // Result: cs from Getting Started to Jingjing </ Title> <author> Waiting </ author> <price> 58.3 </ price> <price> </ employees> 2, add a node: xmlDocument xmldoc = new xmlDocument (); xmldoc.Load (Server.MAppath)); XMLNode root = xmldoc.selectsinglenode ("Employees"); // Find <Employees> XMLELEMENT XE1 = XmLDoc.createElement ("Node"); // Create a <Node> Node XE1.SetAttribute ("Genre", "Zhang 3"); // Set this node GENRE attribute XE1.SetAttribute ("ISBN", "1-1111"); // Set this node ISBN property</p> <p>XMLELEMENT XESUB1 = XMLDoc.createElement ("Title"); Xesub1.innertext = "Getting Started Help"; // Settings Text Node Xe1.Appendchild (Xesub1); // Add to <Node> Node XMLELEMENT XESUB2 = XmLDoc.createElement "author"); Xesub2.innertext = "master"; XE1.Appendchild (Xesub2); XMLELEMENT XESUB3 = XMLDoc.createElement ("price"); XESUB3.INNERTEXT = "158.3"; Xe1.Appendchild (Xesub3);</p> <p>Root.Appendchild (xe1); // Add to <Employees> Node XMLDoc.save (Server.MAppath ("DATA.XML"));</p> <p>/ / The result is added to the original content of XML. The content is as follows, <? XML Version = "1.0" encoding = "GB2312"?> <Employees> <node genre = "赞红" isbn = "2- 3631-4 "> <title> CS from getting started </ title> <author> 捷 </ author> <price> 58.3 </ price> </ node> <node genre =" Li Zanhong "ISBN =" 2- 3631-4> <title> CS From Getting Started to Jing </ Title> <Author> Waiting </ Author> <price> 58.3 </ price> </ node> <node genre = "Zhang San" ISBN = "1 -1111-1> <title> C # Getting Started </ title> <author> Master </ author> <price> 158.3 </ price> </ node> </ employees> 3, modify the value of the node (attributes and sub) Node):</p> <p>XmlDocument Xmldoc = new xmldocument (); xmldoc.load (server.mappath ("data.xml");</p> <p>XMLNodelist NodeList = XmLDoc.selectsinglenode ("Employees"). ChildNodes; // Get all child nodes in the Employees node</p> <p>Foreach (XMLNode Xn In nodelist) // Traverse all child nodes {xmlelement XE = (xmlelement) XN; // convert the child node type to XMLELEMENT type if (Xe.GetaTribute ("genre") == "Zhang San") / / If the GENRE property value is "Zhang 3" {xe.setttribute ("genre", "Update Zhang 3"); // Modify this property is "Update Zhang 3"</p> <p>XMLNodelist NLS = XE.ChildNodes; // Continue all child nodes of the XE child node Foreach (XMLNode XN1 in NLS) // Traversed {XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type IF (Xe2.Name == "AUTHOR ") // If you find {xe2.innerText =" Assembly "; // modify}}}} xmldoc.save (Server.MAppath (" DATA.XML ")); // Save.</p> <p>/ / The information of all the nodes has been modified, and the contents of XML are as follows, <? XML Version = "1.0" encoding = "GB2312"?> <Employees> <node genre = "Li Zanhong" ISBN = "2 -3631-4> <title> CS From Getting Started to Open </ Title> <Author> Waiting </ Author> <price> 58.3 </ price> </ node> <node genre = "赞红" ISBN = "2 -3631-4> <title> CS From Getting Started to Jing </ Title> <Author> Waiting </ Author> <price> 58.3 </ price> </ node> <node genre = "Update Zhang 3" ISBN = "1-1111-1"> <title> C # Getting Started </ title> <author> Yak </ author> <price> 158.3 </ price> </ node> </ employees> 4, modify the node (Add Non-point properties and self-junction of nodes: xmlDocument Xmldoc = new xmldocument (); xmldoc.load (Server.MAppath ("DATA.XML");</p> <p>XMLNodelist NodeList = XmLDoc.selectsinglenode ("Employees"). ChildNodes; // Get all child nodes in the Employees node</p> <p>Foreach (XMLNode Xn In nodelist) {XMLELEMENT XE = (XMLELEMENT) XN; XE.SetAttribute ("TEST", "111111");</p> <p>XMLELEMENT XESUB = XMLDoc.createElement ("flag"); Xesub.innertext = "1"; XE.Appendchild (Xesub);} Xmldoc.save (Server.MAppath ("DATA.XML");</p> <p>/ /: The attribute of each node has added one, the sub-node has also added one, the content is as follows, <? XML Version = "1.0" Encoding = "GB2312"?> <Employees> <node genre = "Li Zanhong "ISBN =" 2-3631-4 "test =" 111111 "> <title> CS from getting started </ title> <author> 捷 </ author> <price> 58.3 </ price> <flag> 1 < / flag> </ node> <node genre = "Li Zanda" ISBN = "2-3631-4" test = "111111"> <title> CS from the entry to the master </ title> <author> 捷 </ author> <price> 58.3 </ price> <flag> 1 </ flag> </ node> <node genre = "Update Zhang 3" ISBN = "1-1111" test = "111111> <title> C # Getting Started </ Title> <author> Yak </ author> <price> 158.3 </ price> <flag> 1 </ flag> </ node> </ employees> 5, delete an attribute in the node: xmlDocument XMLDOC = new XmlDocument (); xmlDoc.Load (Server.MapPath ( "data.xml")); XmlNodeList xnl = xmlDoc.SelectSingleNode ( "Employees") ChildNodes;. foreach (XmlNode xn in xnl) {XmlElement xe = (XmlElement) Xn; Xe.Removettribute ("genre"); // Delete GENRE attribute</p> <p>XMLNodelist NLS = XE.ChildNodes; // Continue all child nodes of the XE child node Foreach (XMLNode XN1 in NLS) // Traversed {XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type IF (XE2.Name == "flag ") // If you find {xe.removechild (xe2); // remove}}} xmldoc.save (Server.MAppath (" DATA.XML "));</p> <p>//] Result: Delete a sub-node of an attribute and node of the node, the content is as follows, <? XML Version = "1.0" encoding = "GB2312"?> <Employees> <node isbn = "2-3631- 4 "Test =" 111111 "> <title> CS From Getting Started> Waiting </ Title> <Author> Waiting </ Author> <price> 58.3 </ price> </ node> <node isbn =" 2-3631- 4 "TEST =" 111111 "> <title> CS from getting started </ title> <author> 捷 </ author> <price> 58.3 </ price> </ node> <node isbn =" 1-1111- 1 "TEST =" 111111 "> <title> C # Getting Started </ title> <author> Ya Sheng </ author> <price> 158.3 </ price> </ node> </ employees> 6, delete node: xmlDocument xmlDoc = new XmlDocument (); xmlDoc.Load (Server.MapPath ( "data.xml")); XmlNode root = xmlDoc.SelectSingleNode ( "Employees"); XmlNodeList xnl = xmlDoc.SelectSingleNode ( "Employees") ChildNodes;. for (INT i = 0; i <xnl.count; i ) {xmlelement XE = (xmlelement) xnl.item (i); if (xe.gettribute ("genre") == "Zhang 3") {root.removechild XE); if (i <xnl.count) i = i-1;}} Xmldoc.save (Server.MAppath ("DATA.XML"));</p> <p>//] Result: Delete all the nodes of the eligible, the original content:</p> <p><? XML Version = "1.0" encoding = "gb2312"?> <Employees> <node genre = "Li Zan Hong" ISBN = "2-3631-4"> <title> CS from entry to master </ title> <author> Waiting </ author> <price> 58.3 </ price> </ node> <node genre = "Li Zanli" ISBN = "2-3631-4"> <title> CS from getting started </ title> <author> Waiting </ author> <price> 58.3 </ price> </ node> <node genre = "Zhang 3" ISBN = "1-1111-1"> <title> C # Getting Started Help </ Title> <author> master </ author> <price> 158.3 </ price> </ node> <node genre = "Zhang 3" ISBN = "1-1111-1"> <title> c # Getting Started </ title> <author> master </ Author> <price> 158.3 </ price> </ node> </ employees> Deleted content: <? xml version = "1.0" encoding = "gb2312"?> <Employees> <node genre = "Li Zan Hong" ISBN = "2-3631-4"> <title> CS from the introductory to the master </ title> <author> 捷 </ author> <price> 58.3 </ price> </ node> <node genre = "李赞红" isbn = "2-3631-4"> <title> CS from getting started </ title> <author> 捷 </ author> <price> 58.3 </ price> </ node> </ employees></p> <p>7, read XML according to the text file</p> <p>System.IO.StreamReader myFile = new System.IO.StreamReader (Server.MapPath ( "data.xml"), System.Text.Encoding.Default); // Note System.Text.Encoding.Defaultstring myString = myFile.ReadToEnd ( ); // mystring is a read string myfile.close ();</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-132326.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="132326" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'rTXSPNbFERJo51ZQ_2FvOywfAzAZyEh0vVKv_2BORibY_2BohxUmd67XkijUH_2FA2Jjga_2Flj4hYMFhizkqy3FAb'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>