<?xml version="1.0" encoding="utf-8"?><birthday> <NO1 id="1"> <date type="a">1</date> <name>2</name> </NO1> <NO2 id="2"> <date type="b">3</date> <name>4</name> </NO2></birthday>
(AddAdd an N03 node. The node has the type attribute, and the N03 Node also has the name of the child node.
Protected void Page_Load (object sender, EventArgs e) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (@ "F: \ XMLFile1.xml"); XmlNode root = xmlDoc. selectSingleNode ("birthday"); // find <birthday> XmlElement xe1 = xmlDoc. createElement ("NO3"); // create a <NO3> node xe1.SetAttribute ("type", "a"); // set the XmlElement xesub1 = xmlDoc attribute of the node type. createElement ("name"); xesub1.InnerText = "C # From getting started to proficient"; // set the text node xe1.AppendChild (xesub1); // Add it to the root node of the <NO3> node. appendChild (xe1); // Add it to the <birthday> node xmlDoc. save (@ "F: \ XMLFile1.xml ");}
Running result:
<? XML version = "1.0" encoding = "UTF-8"?> <Birthday> <No1 id = "1"> <date type = "A"> 1 </date> <Name> 2 </Name> </No1> <NO2 id = "2"> <date type = "B"> 3 </date> <Name> 4 </Name> </NO2> <NO3 type = "A"> <Name> C # From getting started to proficient </Name> </NO3> </birthday>
(DeleteDelete the attributes and sub-levels in NO3.
Xmldocument xmldoc = new xmldocument (); xmldoc. load (@ "F: \ xmlfile1.xml"); xmlnodelist xnl = xmldoc. selectsinglenode ("Birthday "). childnodes; foreach (xmlnode Xn in xnl) {xmlelement Xe = (xmlelement) xn; If (Xe. getattribute ("type") = "A") {Xe. removeall (); // delete all content of the node // Xe. removeattribute ("type"); // Delete the type attribute} xmldoc. save (@ "F: \ xmlfile1.xml"); // save.
Running result:
<?xml version="1.0" encoding="utf-8"?><birthday> <NO1 id="1"> <date type="a">1</date> <name>2</name> </NO1> <NO2 id="2"> <date type="b">3</date> <name>4</name> </NO2> <NO3> </NO3></birthday>
(ChangeIn the left-side navigation pane.
XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (@ "F: \ XMLFile1.xml"); XmlNodeList nodeList = xmlDoc. selectSingleNode ("birthday "). childNodes; // obtain all subnodes of the birthday node foreach (XmlNode xn in nodeList) // traverse all subnodes {XmlElement xe = (XmlElement) xn; // convert the subnode type to the XmlElement type if (xe. getAttribute ("id") = "1") // if the type property value is "1" {xe. setAttribute ("id", "4"); // modify this attribute to "4"} xmlDoc. save (@ "F: \ XMLFile1.xml"); // Save.
Running result:
<?xml version="1.0" encoding="utf-8"?><birthday> <NO1 id="4"> <date type="a">1</date> <name>2</name> </NO1> <NO2 id="2"> <date type="b">3</date> <name>4</name> </NO2> <NO3> </NO3></birthday>
(QueryQueries the outermost layer of a node, including the value of the Id attribute, and displays the text of the child node.
Protected void Page_Load (object sender, EventArgs e) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (@ "F: \ XMLFile1.xml"); XmlNode xn = xmlDoc. selectSingleNode ("birthday"); XmlNodeList xnl = xn. childNodes; foreach (XmlNode xnf in xnl) {XmlElement xe = (XmlElement) xnf; TextBox1.Text = TextBox1.Text + xe. getAttribute ("id"); // display the attribute value XmlNodeList xnf1 = xe. childNodes; foreach (XmlNode xn2 in xnf1) {TextBox2.Text = TextBox2.Text + xn2.InnerText; // display subnode text }}
Running result: the value of TextBox1.Text is 42.
: TextBox2.Text is 1234