我希望能夠做這樣的事情:短跑在XmlAttribute在C#中開始
//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);
}
我選中此:Using - in XML element name等人,但我沒有找到,我可以用一個答案。
CNC中
這是的ccnet.config的爲例: http://ccnet.sourceforge.net/CCNET/Configuring%20the%20Server.html
謝謝!
我會嘗試它作爲一個元素。 – LolCat
是的,它的工作。謝謝 !! – LolCat