Use of SelectSingleNode in C #

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.