Linq To Xml实现XPath通用函数(Silverlight下的XPath)
2010-07-21 09:48
405 查看
熊哥实现的比较通用的类似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 id=""1""><o2 id=""1""><o3>5</o3><o3>6</o3></o2><o2 id=""2""><o3>11</o3><o3>22</o3></o2></o1><o1 id=""2""><o2 id=""1""><o3>111</o3><o3>222</o3></o2><o2 id=""2""><o3 id=""1""><o4>2222</o4><o4>3333</o4></o3></o2></o1></root>";
//var test = Find(data, @"\\o1[@id=1]\o2[@id=1]\o3");
//var test = Find(data, @"\\o1[@id=1]\o2[@id=2]\o3");
//var test = Find(data, @" \\o1[@id=2]\o2[@id=2]\o3[@id=1]\o4");
var test = Find(data, @"\\o1[@id=2]\o2[@id=2]\o3[@id=1]\o4");
if (test != null)
{
foreach (XElement info in test)
{
if (info != null)
Response.Write(info.ToString());
}
}
}
参考: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 id=""1""><o2 id=""1""><o3>5</o3><o3>6</o3></o2><o2 id=""2""><o3>11</o3><o3>22</o3></o2></o1><o1 id=""2""><o2 id=""1""><o3>111</o3><o3>222</o3></o2><o2 id=""2""><o3 id=""1""><o4>2222</o4><o4>3333</o4></o3></o2></o1></root>";
//var test = Find(data, @"\\o1[@id=1]\o2[@id=1]\o3");
//var test = Find(data, @"\\o1[@id=1]\o2[@id=2]\o3");
//var test = Find(data, @" \\o1[@id=2]\o2[@id=2]\o3[@id=1]\o4");
var test = Find(data, @"\\o1[@id=2]\o2[@id=2]\o3[@id=1]\o4");
if (test != null)
{
foreach (XElement info in test)
{
if (info != null)
Response.Write(info.ToString());
}
}
}
相关文章推荐
- Linq To Xml实现类似XPath查询(Silverlight下的XPath)
- XML 搜索和验证(XmlDocument、XPath to XmlDocument、LINQ to XDocument)
- Silverlight 3中如何创建Linq to SQL项目实现MS-SQL数据的操作
- Silverlight对Xml增删改查(Linq To Xml)
- 在silverlight中使用Linq TO XML
- Linq技术二:Linq to XML及xml增删改查的不同实现方式
- 在silverlight中使用Linq TO XML
- linq to xml 中,有什么好的方法实现xml的字符编码吗?
- LINQ to XML实现XML文档的增删改查
- LINQ To Xml:使用XPath
- XPath 和 LINQ to XML 的比较
- Linq to xml API中 XName 的奇怪实现
- LINQ to XML实现XML文档的增删改查
- Linq to xml实现xml数据增删改查
- Linq To Xml、XPath得到两种格式的xml
- 【原创】C# Linq to XML
- 在Linq to sql 和 Entity framework 中使用lambda表达式实现left join
- Linq to SQL Xml Based Mapping
- Linq to Xml 增删查改
- Linq to Xml示例