惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
GbyAI
GbyAI
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
美团技术团队
The Register - Security
The Register - Security
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
量子位
B
Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
L
LangChain Blog
Latest news
Latest news
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
T
Tenable Blog
T
Tor Project blog

博客园 - 徘徊中的海鸟

在Dictionary使用foreach的注意 ADO.NET,连接\连接池问题 - 徘徊中的海鸟 - 博客园 关于sql server占用系统资源的问题 ASP.NET 的内存不足问题 SQL拆分字段 SQL Server 2005数据库日志文件损坏的情况下如何恢复数据库 .net 发现的cache 问题 .NET线程若干问题 HttpContext.Cache 和 HttpRuntime.Cache IIS 6.0中配置HTTP Gzip压缩 pre标签自动换行方案 HttpWebRequest 和 浏览器打开的区别 TestDirector 安装、使用心得 连接数问题 w3wp 进程问题 关于CultureInfo 读取树数据SQL 提高sql的效率 SQL中只获取日期值
生成代码自动加入到解决方案中
徘徊中的海鸟 · 2007-09-27 · via 博客园 - 徘徊中的海鸟

在开发时或者开发生成工具时,我们有定义的框架 ,然后把生成的代码直接加入到解决方案中,解决方案自动更新。

解决方法:
   把生成文件的放入解决方案的对应项目目录,同时更改项目文件,如在类库项目更新 “.csproj”,把生成的文件及路径写入     <ItemGroup> <Compile Include="newnew.cs"  />  </ItemGroup> 中。项目加载时就可以加载文件了。而在Website中不需要加到项目文件中,但WEB应用程序则需要.
用XmlDocument来更改项目文件。

       /// <summary>
        /// 更改解决方案的项目文件
        /// </summary>
        /// <param name="slnPath"></param>
        /// <param name="fileName"></param>
        /// <param name="ifWebsite"></param>
        public void UpdateProjectFile(string slnPath, string slnName,string fileName, bool ifWebsite)
        {

            if (ifWebsite == true)
                return;
            //解决方案名称
            string objName = slnPath + "/" + slnName + "." + bizProjectName + "/" + slnName + "." + bizProjectName + ".csproj";

            if (File.Exists(objName))
            {

                System.Xml.XmlDocument doc = new XmlDocument();
                doc.Load(objName);
                string nameSpace = doc.DocumentElement.Attributes["xmlns"].Value.Trim();
                foreach (XmlNode xmlNode in doc.DocumentElement.ChildNodes)
                {
                    if (xmlNode.NodeType == XmlNodeType.Element)
                    {
                        XmlElement xmlFirstElement = (XmlElement)xmlNode;
                        if (xmlFirstElement.Name == "ItemGroup" && xmlFirstElement.FirstChild.Name == "Compile")
                        {
                            XmlElement newXmlE = doc.CreateElement("", "Compile", nameSpace);
                            newXmlE.SetAttribute("Include", fileName);
                            xmlFirstElement.AppendChild(newXmlE);
                        }
                    }
                }
                doc.Save(objName);
            }
        }
       
    注意:
 
XmlDocument CreateElement时,会自动添加 xmlns="" 的属性,结果为: <Compile Include="newnew.cs" xmlns="" />
这是因为没有声明命名空间,可将这个节点的xmlns,设为与整个文件的相同,则xmlns将不出现.

                            XmlElement newXmlE = doc.CreateElement("Compile");
                            newXmlE.SetAttribute("Include", fileName);
                            xmlFirstElement.AppendChild(newXmlE);
改成了                
                            XmlElement newXmlE = doc.CreateElement("", "Compile", doc.DocumentElement.Attributes["xmlns"].Value);
                            newXmlE.SetAttribute("Include", fileName);
                            xmlFirstElement.AppendChild(newXmlE);