CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 171

171

Chương 5: XML

<cli:firstName>Sally</cli:firstName>

<cli:lastName>Sergeyeva</cli:lastName>

</cli:client>

<ord:orderItem itemNumber="3211"/>

<ord:orderItem itemNumber="1155"/>

</ord:order>

Và chương trình dưới đây sẽ chọn tất cả các thẻ trong không gian tên

http://mycompany/OrderML

:

using System;

using System.Xml;

public class SelectNodesByNamespace {

private static void Main() {

// Nạp tài liệu.

XmlDocument doc = new XmlDocument();

doc.Load("Order.xml");

// Thu lấy tất cả các thẻ đặt hàng.

XmlNodeList matches = doc.GetElementsByTagName("*",

"http://mycompany/OrderML");

// Hiển thị thông tin.

Console.WriteLine("Element \tAttributes");

Console.WriteLine("******* \t**********");

foreach (XmlNode node in matches) {

Console.Write(node.Name + "\t");

foreach (XmlAttribute attribute in node.Attributes) {

Console.Write(attribute.Value + " ");

}

Console.WriteLine();

}