XPath和*.csproj我肯定错过了一些重要的细节。我就是无法让.NET的XPath处理VisualStudio项目文件。我们加载一个xml文档:vardoc=newXmlDocument();doc.Load("blah/blah.csproj");现在执行我的查询:varnodes=doc.SelectNodes("//ItemGroup");Console.WriteLine(nodes.Count);//糟糕,零当然,文件中有名为ItemGroup的节点。此外,此查询有效:varnodes=doc.SelectNodes("//*/@Include");Console.WriteLine(nodes.Count);//发现一些XPath与其他文档一起工作得很好。我对此完全困惑。任何人都可以解释发生了什么事吗?您可能需要添加对命名空间http://schemas.microsoft.com/developer/msbuild/2003的引用。我有一个类似的问题,我在这里写了它。做这样的事情:XmlDocumentxdDoc=newXmlDocument();xdDoc.Load("blah/blah.csproj");XmlNamespaceManagerxnManager=newXmlNamespaceManager(xdDoc.NameTable);xnManager.AddNamespace("tu","http://schemas.microsoft.com/developer/msbuild/2003");XmlNodexnRoot=xdDoc.DocumentElement;XmlNodeListxnlPages=xnRoot.SelectNodes("//tu:ItemGroup",xnManager);查看根命名空间;您必须包含一个xml-命名空间管理器并使用类似“//x:ItemGroup”的查询,其中“x”是您为根命名空间指定的别名。并将经理传递给查询。例如:XmlDocumentdoc=newXmlDocument();doc.Load("my.csproj");XmlNamespaceManagermgr=newXmlNamespaceManager(doc.NameTable);mgr.AddNamespace("foo",doc.DocumentElement.NamespaceURI);XmlNodefirstCompile=doc.SelectSingleNode("//foo:Compile",mgr);我发布了一个LINQ/Xml版本:http://granadacoder.wordpress.com/2012/10/11/how-to-find-references-in-ac-project-file-csproj-using-linq-xml/但那是它的要点。它可能不是100%完美……但它展示了这个想法。我在这里发布代码是因为我在寻找答案时发现了这个(原始帖子)。然后我厌倦了搜索并写了我自己的。以上就是C#学习教程分享的全部内容:XPath和*.csproj。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—stringfileName=@"C:MyFolderMyProjectFile.csproj";XDocumentxDoc=XDocument.Load(文件名);XNamespacens=XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");//References"ByDLL(file)"varlist1=fromlistinxDoc.Descendants(ns+"ItemGroup")fromiteminlist.Elements(ns+"Reference")/*whereitem.Element(ns+"HintPath"")!=null*/selectnew{CsProjFileName=fileName,ReferenceInclude=item.Attribute("Include").Value,RefType=(item.Element(ns+"HintPath")==null)?“CompiledDLLInGac”:“CompiledDLL”,HintPath=(item.Element(ns+“HintPath”)==null)?string.Empty:item.Element(ns+"HintPath").Value};foreach(varvinlist1){Console.WriteLine(v.ToString());}//References"ByProject"varlist2=fromlistinxDoc.Descendants(ns+"ItemGroup")fromiteminlist.Elements(ns+"ProjectReference")whereitem.Element(ns+"ProjectReference")ject")!=nullselectnew{CsProjFileName=fileName,ReferenceInclude=item.Attribute("Include").Value,RefType="ProjectReference",ProjectGuid=item.Element(ns+"Project").Value};foreach(varvinlist2){Console.WriteLine(v.ToString());}本文采集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如有转载请注明出处:
