void NavigateTo(XmlNodeInfo info) { try { this.currentDocument.Show(); TextSpan span = info.CurrentSpan; CodeWindowManager mgr = this.currentDocument.Source.LanguageService.GetCodeWindowManagerForSource(this.currentDocument.Source); IVsTextView view; mgr.CodeWindow.GetLastActiveView(out view); view.SetSelection(span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex); view.EnsureSpanVisible(span); } catch { } }
private TextSpan GetNodeSpan(XmlNodeInfo info) { int line = info.Line; int col = info.Column + 1; // skip '<'. TokenInfo token = this.currentDocument.Source.GetTokenInfo(line, col); TextSpan span = new TextSpan(); if (token != null) { span.iStartLine = span.iEndLine = line; span.iStartIndex = token.StartIndex; span.iEndIndex = token.EndIndex + 1; // include last character of the token. } return span; }
private void XPathQueryButton_Click(object sender, EventArgs e) { try { //ErrorListBox.Items.Clear(); this.errors.Clear(); ; this.resultsGridView.DataSource = null; ClearResults(); GetCurrentSource(); if (!this.currentDocument.IsValidXmlDocument()) { ReportError(new ErrorInfoLine("Invalid XML Document – see Error List Window for details", errors.Count + 1, ErrorInfoLine.ErrorType.Warning)); return; } try { foreach (DataGridViewRow CurrentRow in namespaceGridView.Rows) { if (CurrentRow.Cells[0].Value == null && CurrentRow.Cells[1].Value == null) { //skip empty rows continue; } else { if (CurrentRow.Cells[0].Value == null) { ReportError(new ErrorInfoLine("Invalid Namespace Table - a prefix is required for all namespaces", errors.Count + 1, ErrorInfoLine.ErrorType.Warning)); return; } else { if (CurrentRow.Cells[1].Value == null) { this.currentDocument.XmlNamespaceManager.AddNamespace((string)CurrentRow.Cells[0].Value, string.Empty); } else { this.currentDocument.XmlNamespaceManager.AddNamespace((string)CurrentRow.Cells[0].Value, (string)CurrentRow.Cells[1].Value); } } } } } catch (Exception ex) { //We should never get here //ReportError(ex); return; } XmlNodeList XPathResultNodeList = null; try { XPathResultNodeList = this.currentDocument.Query(xpathTextBox.Text); } catch (XPathException ex) { ReportError(ex); return; } results = new BindingList<XmlNodeInfo>(); if (XPathResultNodeList != null) { foreach (XmlNode Node in XPathResultNodeList) { LineInfo CurrentLineInfo = this.currentDocument.GetLineInfo(Node); if (CurrentLineInfo != null) { XmlNodeInfo info = new XmlNodeInfo(Node, CurrentLineInfo.LineNumber, CurrentLineInfo.LinePosition); results.Add(info); TextSpan span = GetNodeSpan(info); IVsTextLineMarker[] amark = new IVsTextLineMarker[1]; int hr = this.currentDocument.TextEditorBuffer.CreateLineMarker((int)MARKERTYPE2.MARKER_EXSTENCIL, span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex, info, amark); info.Marker = amark[0]; info.MarkerChanged += new EventHandler(OnMarkerChanged); info.MarkerDeleted += new EventHandler(OnMarkerDeleted); } } } this.resultsGridView.AutoGenerateColumns = false; this.resultsGridView.DataSource = results; if (this.results.Count > 0) { this.resultsGridView.Focus(); this.queryTabControl.SelectedTab = this.resultsTabPage; } else { // If no results and XmlDoc has a default namespace, display a warning if (!String.IsNullOrEmpty(this.currentDocument.DocumentElementDefaultNamespace) && !(this.currentDocument.DocumentElementDefaultNamespace == "")) { this.ReportError(new ErrorInfoLine("Document has a default namespace. Did you make sure to add it to the Namespace Table and use its prefix in your XPath query?",errors.Count + 1,ErrorInfoLine.ErrorType.Warning)); return; } } } catch { } }