The XML function is traversed in .NET.

xiaoxiao2021-03-06  41

Today, a function of traversing an XML node is implemented as follows: protected void displaytree (xmlnode node) // Traversing the entire XML function {if (node! = Null) // If Node is not empty {format (node); // Operation of this node} if (node.haschildnodes) // If the node also has a child node {node = node.firstchild; while (node! = Null) {DisplayTree (node); node = node.nextsibling;}}} If you want to do some of this Node, you can implement it in the Format function: void format (system.xml.xmlnode node) {console.writeline (node.name); // Displays the name of each node, if it is a node The value is displayed #text. If (node.name == "company") // If the name of this node is Company {xmlnode nextnode = node.firstnode; // Note, is firstnode, not nextsibingnextsibing, will run Go to the next node; // Displays the value of the node is the value in Company}}} Next: How to get the Node object you need to: In the main function, you can use the DisplayTree function: XmlTextReader Xmlread = New XMLTextReader ("E: //bb.xml"); xmlDocument Xmldoc = new xmldocument (); xmldoc.load (XmlRead); // load parameter can enter the file name, the flow, etc. If the value in Company, you need this: void update (xmlnode node) {if (node.name == "company") {xmlnode nextnode = node.firstchild; Next.Value = "needvalue";}} final, as long as XMLDOC .save ("e: //bb.xml") can be updated, the parameters can be the XMLWRITER object or the file name is written by the detailed code: protected void format {ix (node.name == "Company") // If it is the work unit and position of the Commission {// xmlnode nextnode = node.firstchild;

ListBox1.Items.add (node.innertext); // Displays the value of the Company node} //listbox1.items.add(Node.Name " node.value);} protected void update (xmlnode node) {// update Node's value IF (node.name == "company") {xmlnode nextnode = node.firstchild; nextnode.value = "Lei Xun Company";}}

Protected Void DisplayTree (XMLNode Node) // Traversing the entire XML function {if (node! = null) // If Node is not empty {format (node); // format this Node to output // update (Node) } If (node.haschildnodes) // If the node also has a child node {node = node.firstchild; while (node! = Null) {DisplayTree (node); node = node.nextsibling;}}

}

Private Void Page_Load (Object Sender, System.EventArgs E) {// Place the user code here to initialize the page if (! ispostback) {system.xml.xmltextReader Xmlread = New XMLTextReader ("E: //bb.xml") XMLDocument Xmldoc = new xmldocument (); xmldoc.Load ("E: //bb.xml"); system.xml.xmlnode node = xmldoc.documentelement; displaytree (node); xmlread.close ();

XmlNodeList xmllist = xmldoc.SelectNodes ( "FILE / YEARPLAN"); foreach (XmlNode yearplan in xmllist) {ListBox1.Items.Add (yearplan.InnerText); //yearplan.LastChild.Value="EEEEEE ";} // xmldoc. Save ("e: //bb.xml");}}

转载请注明原文地址:https://www.9cbs.com/read-82637.html

New Post(0)