I want to be able to do something like this :
//buildArgs
XmlNode buildArgs = doc.CreateElement("buildArgs");
XmlAttribute buildArgsAtt = doc.CreateAttribute("-D:project.rc_file");
But I get the fallowing error :
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.
But I did not choose the format. I am trying to automate the process of adding a new element to cruisecontrol.net config file (ccnet.config). So I need to put that dash there.
This is my code :
//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);
}
I checked this : Using - in XML element name and others, but I didn't find an answer that I could use.
-EDIT-
This is an exemple of the ccnet.config : http://ccnet.sourceforge.net/CCNET/Configuring%20the%20Server.html
Thanks!