熊哥实现的比较通用的类似XPath查询函数,记录一下。
参考:Linq To Xml实现类似XPath查询
忘了说明一下:Silverlight不支持XPath查询,只能变通一下了。
public static IEnumerable<XElement> Find(string data, string node)
{
var test = XDocument.Parse(data);
IEnumerable<XElement> find = null;
if (string.IsNullOrEmpty(node) || !node.StartsWith(@"\\")) return null;
string[] list = node.Substring(2).Split('\\');
if (string.IsNullOrEmpty(list[0])) return test.Descendants();
string[] attr = null; string nodeName = string.Empty;
foreach (string info in list)
{
if (info.IndexOf("[") != -1 && info.IndexOf("]") != -1)
{
nodeName = info.Split('[')[0];
attr = info.Split('[')[1].Replace("]", "").Replace("@", "").Split('=');
find = find == null ? test.Descendants(nodeName).Where(w => w.Attribute(attr[0]).Value == attr[1]).ToList() : find.Descendants(nodeName).Where(w => w.Attribute(attr[0]).Value == attr[1]).ToList();
}
else
{
if (find == null) find = test.Descendants();
find.SelectMany(s => s.Elements(info));
}
}
return find;
}
private void F()
{
string data = @"<?xml version=""1.0"" encoding=""utf-8"" ?><root><o1 >)
Response.Write(info.ToString());
}
}
}
{
var test = XDocument.Parse(data);
IEnumerable<XElement> find = null;
if (string.IsNullOrEmpty(node) || !node.StartsWith(@"\\")) return null;
string[] list = node.Substring(2).Split('\\');
if (string.IsNullOrEmpty(list[0])) return test.Descendants();
string[] attr = null; string nodeName = string.Empty;
foreach (string info in list)
{
if (info.IndexOf("[") != -1 && info.IndexOf("]") != -1)
{
nodeName = info.Split('[')[0];
attr = info.Split('[')[1].Replace("]", "").Replace("@", "").Split('=');
find = find == null ? test.Descendants(nodeName).Where(w => w.Attribute(attr[0]).Value == attr[1]).ToList() : find.Descendants(nodeName).Where(w => w.Attribute(attr[0]).Value == attr[1]).ToList();
}
else
{
if (find == null) find = test.Descendants();
find.SelectMany(s => s.Elements(info));
}
}
return find;
}
private void F()
{
string data = @"<?xml version=""1.0"" encoding=""utf-8"" ?><root><o1 >)
Response.Write(info.ToString());
}
}
}