protected List <GrepMatch> DoXPathSearch(int lineNumber, int filePosition, string text, string searchXPath, GrepSearchOption searchOptions, bool includeContext) { List <GrepMatch> results = new List <GrepMatch>(); // skip files that are obviously not xml files, but report errors on badly formed xml int firstElemIdx = text.IndexOf('<'); int firstCharIdx = text.TakeWhile(char.IsWhiteSpace).Count(); if (firstElemIdx > -1 && firstElemIdx <= firstCharIdx) { List <XPathPosition> positions = new List <XPathPosition>(); using (StringReader reader = new StringReader(text)) { // Code from http://stackoverflow.com/questions/10606534/how-to-search-xml-files-with-xpath-returning-line-and-column-numbers-of-found XPathDocument xmlDoc = new XPathDocument(reader); var xpn = xmlDoc.CreateNavigator(); xpn.MoveToFollowing(XPathNodeType.Element); var xns = xpn.GetNamespacesInScope(XmlNamespaceScope.All); var xmngr = new XmlNamespaceManager(xpn.NameTable); foreach (var key in xns.Keys) { xmngr.AddNamespace(key, xns[key]); } var xpni = xpn.Select(searchXPath, xmngr); int foundCounter = 0; while (xpni.MoveNext()) { foundCounter++; var xn = xpni.Current; var xpathPositions = new XPathPosition(); if (xn.NodeType == System.Xml.XPath.XPathNodeType.Attribute) { xpathPositions.EndsOnAttribute = true; xpathPositions.AttributeName = xn.Name; } List <int> currentPositions = new List <int>(); GetXpathPositions(xn, ref currentPositions); if (xpathPositions.EndsOnAttribute) { currentPositions.RemoveAt(currentPositions.Count - 1); } xpathPositions.Path = currentPositions; positions.Add(xpathPositions); } } results.AddRange(GetFilePositions(text, positions)); } return(results); }
protected List <GrepSearchResult.GrepMatch> doXPathSearch(int lineNumber, string text, string searchXPath, GrepSearchOption searchOptions, bool includeContext) { List <GrepSearchResult.GrepMatch> results = new List <GrepSearchResult.GrepMatch>(); // Check if file is an XML file if (text.Length > 5 && text.Substring(0, 5).ToLower() == "<?xml") { List <XPathPosition> positions = new List <XPathPosition>(); using (StringReader reader = new StringReader(text)) { // Code from http://stackoverflow.com/questions/10606534/how-to-search-xml-files-with-xpath-returning-line-and-column-numbers-of-found XPathDocument xmlDoc = new XPathDocument(reader); var xpn = xmlDoc.CreateNavigator(); xpn.MoveToFollowing(XPathNodeType.Element); var xns = xpn.GetNamespacesInScope(XmlNamespaceScope.All); var xmngr = new XmlNamespaceManager(xpn.NameTable); foreach (var key in xns.Keys) { xmngr.AddNamespace(key, xns[key]); } var xpni = xpn.Select(searchXPath, xmngr); int foundCounter = 0; while (xpni.MoveNext()) { foundCounter++; var xn = xpni.Current; var xpathPositions = new XPathPosition(); if (xn.NodeType == System.Xml.XPath.XPathNodeType.Attribute) { xpathPositions.EndsOnAttribute = true; xpathPositions.AttributeName = xn.Name; } List <int> currentPositions = new List <int>(); getXpathPositions(xn, ref currentPositions); if (xpathPositions.EndsOnAttribute) { currentPositions.RemoveAt(currentPositions.Count - 1); } xpathPositions.Path = currentPositions; positions.Add(xpathPositions); } } results.AddRange(getFilePositions(text, positions)); } return(results); }
protected List<GrepSearchResult.GrepMatch> doXPathSearch(string text, string searchXPath, GrepSearchOption searchOptions, bool includeContext) { List<GrepSearchResult.GrepMatch> results = new List<GrepSearchResult.GrepMatch>(); // Check if file is an XML file if (text.Length > 5 && text.Substring(0, 5).ToLower() == "<?xml") { List<XPathPosition> positions = new List<XPathPosition>(); using (StringReader reader = new StringReader(text)) { // Code from http://stackoverflow.com/questions/10606534/how-to-search-xml-files-with-xpath-returning-line-and-column-numbers-of-found XPathDocument xmlDoc = new XPathDocument(reader); var xpn = xmlDoc.CreateNavigator(); xpn.MoveToFollowing(XPathNodeType.Element); var xns = xpn.GetNamespacesInScope(XmlNamespaceScope.All); var xmngr = new XmlNamespaceManager(xpn.NameTable); foreach (var key in xns.Keys) xmngr.AddNamespace(key, xns[key]); var xpni = xpn.Select(searchXPath, xmngr); int foundCounter = 0; while (xpni.MoveNext()) { foundCounter++; var xn = xpni.Current; var xpathPositions = new XPathPosition(); if (xn.NodeType == System.Xml.XPath.XPathNodeType.Attribute) { xpathPositions.EndsOnAttribute = true; xpathPositions.AttributeName = xn.Name; } List<int> currentPositions = new List<int>(); getXpathPositions(xn, ref currentPositions); if (xpathPositions.EndsOnAttribute) currentPositions.RemoveAt(currentPositions.Count - 1); xpathPositions.Path = currentPositions; positions.Add(xpathPositions); } } results.AddRange(getFilePositions(text, positions)); } return results; }