C#中忽略大小寫在XML中查詢
來源:互聯網
上載者:User
/**//// <summary>
/// 不分大小寫查詢XML節點,簡單寫了一下,不知道效率怎麼樣。
/// 從MSDN是摘了點代碼,呵呵。
/// </summary>
static void CompareXmlIgnoreCase()
{
string xml = "<root><item1 type='a'>First</item1><ITEM2 type='A'>Second</ITEM2><ITEM1 type='A'>SECOND</ITEM1></root>";
XmlDocument _doc = new XmlDocument();
_doc.LoadXml(xml);
//按名稱查詢
XmlNodeList l = _doc.DocumentElement.SelectNodes("//root/node()[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'item1']");
for (int i = 0; i < l.Count; i++)
{
Console.WriteLine(l[i].OuterXml);
}
Console.WriteLine("===================================================");
//按某個屬性比較查詢
l = _doc.DocumentElement.SelectNodes("//root/node()[translate(@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'a']");
for (int i = 0; i < l.Count; i++)
{
Console.WriteLine(l[i].OuterXml);
}
Console.WriteLine("===================================================");
//按InnerText比較查詢
l = _doc.DocumentElement.SelectNodes("//root/node()[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'second']");
for (int i = 0; i < l.Count; i++)
{
Console.WriteLine(l[i].OuterXml);
}
}