Record the usage of SelectSingleNode in C #.
Here there are two XML files, one described by attributes and the other described by nodes. These two operations are also different.
I
<Ad>
<Adlist name = "ad 1" pic = "UpFile/1.jpg" url =" http://www.sohu.com "rank =" 1 "/>
<Adlist name = "Ad 2" pic = "UpFile/2.jpg" url =" http://www.163.com "rank =" 2 "/>
<Adlist name = "ad 3" pic = "UpFile/2.jpg" url =" http://www.163.com "rank =" 3 "/>
</Ad>
II
<Ad>
<Adlist>
<Ad_name> advertisement 1 </ad_name>
<Ad_pic> UpFile/1.jpg</ad_pic>
<Ad_url> http://www.sohu.com </ad_url>
<Ad_qz> 2 </ad_qz>
</Adlist>
<Adlist>
<Ad_name> advertisement 2 </ad_name>
<Ad_pic> UpFile/2.jpg</ad_pic>
<Ad_url> http://www.163.com </ad_url>
<Ad_qz> 1 </ad_qz>
</Adlist>
</Ad>
The backend bindings are the same and all are bound to adlist.
XmlDataSource xmlSource = new XmlDataSource ();
XmlSource. XPath = "Ad/adlist ";
On the page, I use Repeater to display
For the first type, the Eval binding expression <% # Eval ("name") %> name is the attribute of adlist.
For the second type, use the XPath data binding expression <% # XPath ("ad_name") %> ad_name to be the subnode under adlist
Modify:
For the first type of XML, use the attribute name to find the node, convert the node to XmlElement, and set the attribute. xmlDoc is an instance of XmlDocument.
XmlNode xFind = xmlDoc. SelectSingleNode ("Ad/adlist [@ name = '" + sName + "']");
XmlElement element = (XmlElement) xFind;
Element. SetAttribute ("attribute name", "Modify value ");
After modification, Save the modification. Otherwise, the value is only saved in the memory and not written to the file. xmlDoc. Save (PATH );
For the second type of XML, I use XPathNavigator to operate. xmlDoc.
XmlNode xFind = xmlDoc. SelectSingleNode ("Ad/adlist [ad_name = '" + sName + "']");
XPathNavigator nav = xFind. CreateNavigator ();
XPathNodeIterator ite = nav. Select ("ad_name"); // search for www.2cto.com
If (ite! = Null & ite. MoveNext () // if it is not NULL, move it to the next node. The next node is a text node.
{
Ite. Current. SetValue ("ad 3"); // you can specify the value of the Current node.
}
The last step is xmlDoc. Save (PATH );
Author: zhuzhu837_1