1

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!

Community
  • 1
  • 1
LolCat
  • 539
  • 1
  • 11
  • 24

2 Answers2

3

You simply can't, that's not a valid attribute name. You can store it as attribute value though - just use a name that XML allows.

I'm pretty sure CruiseControl.Net is not doing this either, as it couldn't use the built-in parser on such file.

EDIT: it's element value (also called content). Set it as follows:

var buildArgs = doc.CreateElement("buildArgs");
buildArgs.Value = "-D:project.rc_file";
skolima
  • 31,963
  • 27
  • 115
  • 151
  • Well ... http://ccnet.sourceforge.net/CCNET/Configuring%20the%20Server.html If you look at the you will see the dash D (-D) ... – LolCat May 28 '12 at 12:58
  • This answer is also correct, since it was not an attribute, but an element. Thanks. – LolCat May 28 '12 at 13:26
  • 1
    @LolCat the key point is not the "it was an element not an attribute" (as a name starting dash would still be invalid with an element) - the key point is "it is the value, not the name" – Marc Gravell May 28 '12 at 13:40
  • @Marc Gravell Yes, you are right. It's ok because it became the value of the element and not the name of the element or the attribute. XML won't allows dash there. Thanks for the specification. – LolCat May 28 '12 at 14:06
1

Which line of the example config? Do you mean this one?

<buildArgs>-D:cvs.executable=c:\putty\cvswithplinkrsh.bat</buildArgs>

That's not an attribute. It's element content.

Tom W
  • 5,108
  • 4
  • 30
  • 52