tags:

views:

154

answers:

3

I want to search csproj file for a particular string through code. How can I do it ?

Thanks.

A: 

.csproj files are XML documents so XPath would seem like a suitable vehicle for your needs. You can find an introduction with examples here.. XPath is supported on a wide range of platforms including .NET (example here).

This might be overkill for your needs in which case a regular expression might be just what you are looking for (there are tons of tutorials out there).

Just curious, what are you trying to achieve?

In .NET you might write something like this:

XPathDocument Doc = new XPathDocument("foo.csproj);
XPathNavigator navigator = Doc.CreateNavigator();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(navigator.NameTable);
namespaceManager.AddNamespace("pr", "http://schemas.microsoft.com/developer/msbuild/2003");
XPathNodeIterator iterator = navigator.Select(@"pr:Project/pr:ItemGroup/pr:Compile[@Include='AssemblyInfo.cs']", namespaceManager);

while (iterator.MoveNext())
{
   // Do something interesting
}
Paul Arnold
+1  A: 

it's just an xml text file. Use any xml or text file technology to read it.

e.g. in c#

string textToFind="someText";
string text = File.ReadAllLines("xxx.csproj");
if(text.Contains(textToFind)) Console.WriteLine("found it");
Preet Sangha
A: 

try this:

using(StreamReader reader = new StreamReader("Project1.csproj"))
{
   string criteria = "sample";
   string line = "";
   int ln = 0;
   while((line=reader.readLine()) != null)
   {
    int col = line.indexOf(criteria);
    if(col != -1)
    Console.WriteLine(criteria + " is found in line: " + ln + " col: " + col);
    ln++;
   }
}
jerjer