C#中忽略大小写在XML中查询
/// <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);
}
}
/// 不分大小写查询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);
}
}