爲什麼每次我嘗試向節點添加新屬性時,它都會添加一個額外的屬性"xmlns="
?任何方式來防止這種情況?xml文檔,設置屬性問題
public void changeProjToAssembly(string projPath,string projRefName)
{
XmlDocument doc = new XmlDocument();
doc.Load(projPath);
XmlNode projectReferenceNode;
XmlNode itemGroupNode;
XmlNode root = doc.DocumentElement;
string s = doc.DocumentElement.GetNamespaceOfPrefix("");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("rs", s);
projectReferenceNode = root.SelectSingleNode("/rs:Project/rs:ItemGroup/rs:ProjectReference[rs:Name='GS_POSLibrary1']", nsmgr);
itemGroupNode = root.SelectSingleNode("/rs:Project/rs:ItemGroup[rs:ProjectReference/rs:Name='GS_POSLibrary1']", nsmgr);
XmlElement newCd = doc.CreateElement("Reference");
newCd.SetAttribute("Include", "dasdsad");
newCd.InnerXml = "<HintPath>" + ".//sadssa/asdsad" + "</HintPath>"+
"<HintPath>" + ".//sadssa/asdsad" + "</HintPath>" +
"<HintPath>" + ".//sadssa/asdsad" + "</HintPath>";
itemGroupNode.ReplaceChild(newCd, projectReferenceNode);
Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);
}
XML:
<ItemGroup>
<ProjectReference Include="..\common\librarycomponents\exportdb\GenerateDocLibrary.vbproj">
<Project>{9B3C9E8B-436B-4A16-87A8-E72CB2FFC6E6}</Project>
<Name>GS_POSLibrary</Name>
<Private>False</Private>
</ProjectReference>
<Reference Include="dasdsad" xmlns="">//I only need the Include Atrribute
<HintPath>.//sadssa/asdsad</HintPath>
<HintPath>.//sadssa/asdsad</HintPath>
<HintPath>.//sadssa/asdsad</HintPath>
</Reference>
也許你可以顯示你的代碼?你如何期待我們知道爲什麼你的代碼添加了'xmlns'屬性而不顯示你的代碼,即使我們有線索?似乎與我矛盾。 – 2010-11-16 19:02:43
對不起,我現在已經添加了代碼,我試圖通過直接編輯它的xml內容來編輯vbproj文件中的項目引用,作爲程序集引用進行更改 – 3ggerhappy 2010-11-16 19:08:50
請顯示生成的XML。另外,請說明你爲什麼關心xmlns屬性。 – 2010-11-16 19:18:44