我想通过代码在csproj文件中搜索特定的字符串。我该怎么办?
感谢。
答案 0 :(得分:1)
.csproj文件是XML文档,因此XPath似乎是适合您需求的工具。你可以找到一个introduction with examples here.。 XPath在包括.NET(example here)在内的各种平台上得到支持。
这可能对你的需求有些过分,在这种情况下,正则表达式可能就是你正在寻找的东西(有大量的教程)。
好奇,你想要实现什么目标?
在.NET中你可能会写这样的东西:
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
}
答案 1 :(得分:1)
它只是一个xml文本文件。使用任何xml或文本文件技术来阅读它。
e.g。在c#
string textToFind="someText";
string text = File.ReadAllLines("xxx.csproj");
if(text.Contains(textToFind)) Console.WriteLine("found it");
答案 2 :(得分:0)
试试这个:
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++;
}
}