Dash at the start of XmlAttribute in C#

martindoan

New Member
I want to be able to do something like this : \[code\] //buildArgs XmlNode buildArgs = doc.CreateElement("buildArgs"); XmlAttribute buildArgsAtt = doc.CreateAttribute("-D:project.rc_file");\[/code\]But I get the fallowing error :\[code\]An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dllAdditional information: Invalid name character in '-D'. The '-' character, hexadecimal value 0x2D, cannot be included in a name.\[/code\]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 : \[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 = http://stackoverflow.com/questions/10784952/projectName +" " + oracleVersion; projet.Attributes.SetNamedItem(projetAtt); ... //buildArgs XmlNode buildArgs = doc.CreateElement("buildArgs"); XmlAttribute buildArgsAtt = doc.CreateAttribute("-D:project.rc_file"); buildArgsAtt.Value = http://stackoverflow.com/questions/10784952/projectName +".rc"; XmlAttribute buildArgsAtt2 = doc.CreateAttribute("-D:project.svn_trunk_ver"); buildArgsAtt2.Value = http://stackoverflow.com/questions/10784952/trunkNb; XmlAttribute buildArgsAtt3 = doc.CreateAttribute("-D:project.svn_trunk"); buildArgsAtt3.Value = http://stackoverflow.com/questions/10784952/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); }\[/code\]I checked this : http://stackoverflow.com/questions/1478486/using-in-xml-attribute-name Thanks!
 
Back
Top