首页 新闻 会员 周边

C#解析.csproj

0
悬赏园豆:20 [已解决问题] 解决于 2013-12-19 10:22

现在的需求是根据一个指定的.csproj文件,来解析里面的元素,找到C#工程的Release路径。

虽然知道.csproj是一个XML文件,但是不想写解析XML的类,最好能使用C#自带的类来解析就好了。

查找资料发现Microsoft.Build.Evaluation名空间下Project类能够在一定程序上解析C#工程文件,但好像解析得不完全,最明显的是能够获得Debug的路径,但是却获得不了Release的路径。

 

不知道哪位高手做过此方面的工作,请赐教!

luoshupeng的主页 luoshupeng | 初学一级 | 园豆:68
提问于:2013-04-23 23:14
< >
分享
最佳答案
0

                    //打开工程集文件
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strFilePath);
                    XmlNodeList xnl = doc.ChildNodes[0].ChildNodes;
                    if (doc.ChildNodes[0].Name.ToLower() != "project")
                        xnl = doc.ChildNodes[1].ChildNodes;

                    string assemblyname = string.Empty;//程序集名
                    string outputpath = string.Empty;//输出路径
                    //遍历工程集文件
                    foreach (XmlNode xn in xnl)
                    {
                        if (xn.ChildNodes.Count > 0 && xn.ChildNodes[0].Name.ToLower() == "configuration")
                        {
                            foreach (XmlNode cxn in xn.ChildNodes)
                            {
                                if (cxn.Name.ToLower() == "assemblyname")
                                {
                                    assemblyname = cxn.InnerText;
                                }
                            }
                        }

                        if (xn.ChildNodes.Count > 0 && xn.ChildNodes[0].Name.ToLower() == "debugsymbols")
                        {
                            foreach (XmlNode cxn in xn.ChildNodes)
                            {
                                if (cxn.Name.ToLower() == "outputpath")
                                {
                                    outputpath = cxn.InnerText.EndsWith("\\") ? cxn.InnerText.Substring(0, cxn.InnerText.Length - 1) : cxn.InnerText;
                                    if (string.IsNullOrEmpty(outputpath) || !outputpath.ToUpper().Contains("EAPPRORESOURCE"))
                                    {
                                        MessageBox.Show("工程:" + tn.Text + " 的输出路径设置不对," + outputpath + "应设置到对应的EAPPRORESOURCE文件夹下的对应业务文件存放的文件夹中。");
                                        return;
                                    }
                                    outputpath = outputpath.Substring(outputpath.ToUpper().IndexOf("EAPPRORESOURCE") , outputpath.Length - outputpath.ToUpper().IndexOf("EAPPRORESOURCE") );
                                    outputpath = outputpath.Substring(outputpath.IndexOf("\\")+1 , outputpath.Length - outputpath.IndexOf("\\") -1);
                                }                              
                            }
                        }
                    }
                    sbFiles.Append("|" + outputpath +"\\" +  assemblyname+".dll");
                }

收获园豆:20
五行缺木 | 菜鸟二级 |园豆:386 | 2013-04-24 08:28

你的这个方法也很好,只是不想使用这种解析XML的方法。

我想看看C#自带的类是否能够完成这个工作。

luoshupeng | 园豆:68 (初学一级) | 2013-04-24 08:41

@luoshupeng: System.Xml.XmlDocument本来就是C#自带的类库

五行缺木 | 园豆:386 (菜鸟二级) | 2013-04-24 08:45

@五行缺木: 这个是,你看看我用的那个Project类,这个类可以得出一部分内容的。我的意思是类似这样的类

luoshupeng | 园豆:68 (初学一级) | 2013-04-24 08:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册