w3schools 網站對其函式的說明: Converts string1 by replacing the characters in string2 with the characters in string3.
利用這個函式, 我們可以進行不區分大小寫的查詢 (如下):
//查出所有的<item>, 且不分大小寫 string strXml = "<root><item>111</item><item><Item>222</Item></item></root>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(strXml); //只會查root下一層的<item> XmlNodeList list = doc.SelectNodes( "/root/*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='item']"); foreach (XmlNode node in list) { Console.WriteLine(node.OuterXml); } //會查root下所有層的<item> list = doc.SelectNodes( "/root//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='item']"); foreach (XmlNode node in list) { Console.WriteLine(node.OuterXml); } //查出name屬性值為cd的<item> strXml = "<root><item name=\"cd\">111</item><item name=\"CD\">222</item></root>"; doc.LoadXml(strXml); list = doc.SelectNodes( "//item[translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='cd']"); foreach (XmlNode node in list) { Console.WriteLine(node.OuterXml); }上述的例子是將欲查詢的對象全部轉成小寫, 然後再比對是否符合.
若要全部轉大寫, 就要用
translate(@name, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
沒有留言:
張貼留言