问题描述
我正在尝试编写一些代码以使用C#读取* .CSPROJ文件
I am attempting to write some code to read in a *.CSPROJ file using C#
我的代码如下
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(fullPathName);
XmlNamespaceManager mgr = new XmlNamespaceManager(xmldoc.NameTable);
//mgr.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
foreach (XmlNode item in xmldoc.SelectNodes("//EmbeddedResource") )
{
string test = item.InnerText.ToString();
}
使用调试器,我可以看到'fullPathName'具有正确的值,并且加载后的xmldoc具有正确的内容。
using the debugger I can see that 'fullPathName" has the correct value and the xmldoc once loaded has the correct contents.
尽管xmldoc没有任何节点,就好像该内容未被识别为XML。
The xmldoc does not have any "Nodes" though, as if the contents are not recognised as XML.
使用XML编辑器* .csproj文件可验证XML文档。
Using a XML editor the *.csproj file validates an XML document.
我要去哪里错了?
推荐答案
您已经接近XmlNamespaceManager的添加,但是没有在SelectNodes方法中使用它:
You were getting close with your XmlNamespaceManager addition, but weren't using it in the SelectNodes method:
XmlNamespaceManager mgr = new XmlNamespaceManager(xmldoc.NameTable);
mgr.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
foreach (XmlNode item in xmldoc.SelectNodes("//x:ProjectGuid", mgr))
{
string test = item.InnerText.ToString();
}
(我切换为搜索其他元素,因为我的项目没有没有任何嵌入式资源)
(I switched to searching for a different element as my project didn't have any embedded resources)
这篇关于在C#中读取* .CSPROJ文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!