2013-03-21 3 views
4

전진 전용 리더 System.Xml.XmlTextReader을 사용하고 있습니다. 디버깅 할 때 언제든지 커서의 행 번호와 열 번호를 확인하기 위해 LineNumberLinePosition 속성을 확인할 수 있습니다. 문서에서 커서의 "경로"를 볼 수있는 방법이 있습니까?문서 커서의 경로는 무엇입니까?

예를 들어, 다음 HTML 문서에서 커서가 * 인 경우 경로는 html/body/p과 같을 것입니다. 나는 이것이 정말 도움이되는 것을 발견 할 것이다.

<html> 
    <head> 
    </head> 
    <body> 
     <p>*</p> 
    </body> 
</html> 

편집 : 나는 또한 XmlWriter을 검사 할 수 있기를 바랍니다.

답변

2

내가 알기로, 일반 XmlTextReader로는 그렇게 할 수 없다. 그러나 새로운 Path 속성을 통해이 기능을 제공하도록 확장 할 수 있습니다.

public class XmlTextReaderWithPath : XmlTextReader 
{ 
    private readonly Stack<string> _path = new Stack<string>(); 

    public string Path 
    { 
     get { return String.Join("/", _path.Reverse()); } 
    } 

    public XmlTextReaderWithPath(TextReader input) 
     : base(input) 
    { 
    } 

    // TODO: Implement the other constuctors as needed 

    public override bool Read() 
    { 
     if (base.Read()) 
     { 
      switch (NodeType) 
      { 
       case XmlNodeType.Element: 
        _path.Push(LocalName); 
        break; 

       case XmlNodeType.EndElement: 
        _path.Pop(); 
        break; 

       default: 
        // TODO: Handle other types of nodes, if needed 
        break; 
      } 

      return true; 
     } 

     return false; 
    } 
}