Cấp bậc tác giả:

DOTNET

Xử lý XML trong lập trình C# .NET - Phần V

Được viết bởi QuangIT ngày 20/09/2012 lúc 03:01 PM
Ở kỳ 4, bạn đã được biết qua chức năng của phương thức SelectNodes() của class XmlDocument, nhưng cũng có nhiều lúc bạn chỉ muốn chọn một nút mà thôi.
  • 0
  • 8718

Xử lý XML trong lập trình C# .NET - Phần V

KỲ 5

CHỌN MỘT NÚT BẤT KỲ BẰNG CÁCH SỬ DỤNG PHƯƠNG THỨC SelectSingleNode()


Ở kỳ 4, bạn đã được biết qua chức năng của phương thức SelectNodes() của class XmlDocument, nhưng cũng có nhiều lúc bạn chỉ muốn chọn một nút mà thôi. Tất nhiên là vẫn có thể dùng SelectNodes(), tuy nhiên có vẻ không hợp lý lắm. Bởi thế, class XmlNode còn cung cấp cho bạn phương thức SelectSingleNode() cho việc chọn 1 nút.


Để dễ dàng theo dõi, mình post lại nội dung của tập tin employees.xml:


<?xml version="1.0" encoding="utf-8" ?>

   <!-- This is list of employees -->

   <employees>

     <employee employeeid="1">

       <firstname>Nancy</firstname>

       <lastname>Davolio</lastname>

       <homephone>(206) 555-9857</homephone>

       <notes>

         <![CDATA[includes a BA in psychology from Colorado State University in She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International.]]>

    </notes>

    </employee>

    <employee employeeid="2">

      <firstname>Andrew</firstname>

      <lastname>Fuller</lastname>

      <homephone>(206) 555-9482</homephone>

      <notes>

        <![CDATA[Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in  He is fluent in French and Italian and reads German. He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.]]>

      </notes>

    </employee>

    <employee employeeid="3">

      <firstname>Janet</firstname>

      <lastname>Leverling</lastname>

      <homephone>(206) 555-3412</homephone>

      <notes>

        <![CDATA[Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management.  Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.]]>

      </notes>

    </employee>

  </employees>


Ví dụ minh họa của kỳ này: chúng ta sẽ sửa đổi 1 chút xíu ở ví dụ minh họa kỳ 4, vì đơn giản SelectSingleNode() cũng ko khác chi mấy so với SelectNodes(). Bạn chỉ cần sửa lại đoan code ở những phần sau:

- Thay thế biến thành viên:



XmlNodeList nodeList;  

bằng biến thành viên này:


XmlNode nodeSelect;  

- Thay thế đoạn mã thụ lý tình huống Click của nút "Tìm" bằng đoạn mã mới này:

private void Button1_Click(object sender, System.EventArgs e)

 {

     XmlDocument doc = new XmlDocument();

     doc.Load(Application.StartupPath + "\\employees.xml");

     if (RadioButton1.Checked == true) {

         nodeSelect = doc.SelectSingleNode("/employees/employee[./firstname/text() = '" + TextBox1.Text + "']");

     }

     if (RadioButton2.Checked) {

         nodeSelect = doc.SelectSingleNode("/employees/employee[./lastname/text() = '" + TextBox1.Text + "']");

     }

     if (nodeSelect != null) {

         ComboBox1.Items.Add(nodeSelect.Attributes("employeeid").Value);

     }

     ComboBox1.SelectedIndex = 0;

 }


-Thay thế đoạn mã thụ lý tình huống Click của nút "Hiển thị" bằng đoạn mã mới này:

private void Button2_Click(object sender, System.EventArgs e)

 {

     Label8.Text = nodeSelect.ChildNodes(0).InnerText;

     Label9.Text = nodeSelect.ChildNodes(1).InnerText;

     Label10.Text = nodeSelect.ChildNodes(2).InnerText;

     Label11.Text = nodeSelect.ChildNodes(3).InnerText;

 }


Quả thật, không có chi khó hiểu với phương thức này, chỉ nhớ là SelectSingleNode() chỉ cho ta chọn 1 nút mà thôi! Do đó, nếu có nhập "Nancy" và trong tài liệu có nhiều nhân viên thì cũng chỉ nhận được 1 nhân viên tên Nancy mà tài liệu tìm thấy trước tiên thôi!

Nguồn bài viết: Dngaz.com

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML