我希望能够做这样的事情:
//buildArgs
XmlNode buildArgs = doc.CreateElement("buildArgs");
XmlAttribute buildArgsAtt = doc.CreateAttribute("-D:project.rc_file");
但我得到了休闲错误:
An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dll
Additional information: Invalid name character in '-D'. The '-' character, hexadecimal value 0x2D, cannot be included in a name.
但我没有选择格式。我正在尝试自动化向 Cruisecontrol.net 配置文件 (ccnet.config) 添加新元素的过程。所以我需要把那个破折号放在那里。
这是我的代码:
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
//load from file
doc.Load(filename);
//create node and add value
XmlNode projet = doc.CreateNode(XmlNodeType.Element, "projet", null);
XmlAttribute projetAtt = doc.CreateAttribute("name");
projetAtt.Value = projectName + " " + oracleVersion;
projet.Attributes.SetNamedItem(projetAtt);
...
//buildArgs
XmlNode buildArgs = doc.CreateElement("buildArgs");
XmlAttribute buildArgsAtt = doc.CreateAttribute("-D:project.rc_file");
buildArgsAtt.Value = projectName + ".rc";
XmlAttribute buildArgsAtt2 = doc.CreateAttribute("-D:project.svn_trunk_ver");
buildArgsAtt2.Value = trunkNb;
XmlAttribute buildArgsAtt3 = doc.CreateAttribute("-D:project.svn_trunk");
buildArgsAtt3.Value = trunkPath;
buildArgs.Attributes.SetNamedItem(buildArgsAtt);
buildArgs.Attributes.SetNamedItem(buildArgsAtt2);
buildArgs.Attributes.SetNamedItem(buildArgsAtt3);
//add to parent node
projet.AppendChild(nodeWD);
projet.AppendChild(category);
projet.AppendChild(trigger);
trigger.AppendChild(intTrigger);
projet.AppendChild(sourcecontrol);
sourcecontrol.AppendChild(trunkUrl);
sourcecontrol.AppendChild(workingDirectory);
projet.AppendChild(tasks);
tasks.AppendChild(nant);
nant.AppendChild(targetList);
targetList.AppendChild(target);
nant.AppendChild(buildArgs);
//add to elements collection
doc.DocumentElement.AppendChild(projet);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.Encoding = Encoding.UTF8;
using (XmlWriter writer = XmlTextWriter.Create(filename, settings))
{
doc.Save(writer);
}
我检查了这个:在 XML 元素名称和其他中使用 - ,但我没有找到可以使用的答案。
-编辑-
这是 ccnet.config 的一个例子:http: //ccnet.sourceforge.net/CCNET/Configuring%20the%20Server.html
谢谢!