/// <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将不出现.