熊哥实现的比较通用的类似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 = nullstring 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());
        }
    }
}

 

 

相关文章:

  • 2021-12-21
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2018-04-10
  • 2021-08-30
  • 2021-11-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-19
  • 2021-09-13
  • 2021-08-16
  • 2022-03-07
  • 2021-09-22
  • 2021-07-31
相关资源
相似解决方案