2010-05-10 1 views

답변

0

나는 당신이 원하는 것을 정말로 모르겠다. 그러나 이것은 시작해야 할 몇 가지 일이 될 수있다.
확장 메소드는 정확한 요소를보다 정확하게 지정하기 위해 xpath를 속성이있는 XElement 노드로 가져옵니다.

public static string ToXPath(this XElement element) 
{ 
    var current = element.Parent; 
    string result = ""; 
    while (current != null) 
    { 
     string currentDef = current.Name.ToString(); 
     string attribsDef = ""; 
     foreach (var attrib in current.Attributes()) 
     { 
      attribsDef += " and @" + attrib.Name + "='" + attrib.Value + "'"; 
     } 
     if (attribsDef.Length > 0) 
     { 
      currentDef += "[" + attribsDef.Substring(5) + "]"; 
     } 
     result = "/" + currentDef + result; 
     current = current.Parent; 
    } 
    return result.Substring(1); 
} 
관련 문제