我希望能够做这样的事情:

 //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和其他,但是没有找到我可以使用的答案。

-编辑-

这是ccnet.config的一个示例:
http://ccnet.sourceforge.net/CCNET/Configuring%20the%20Server.html

谢谢!

最佳答案

示例配置的哪一行?你是这个意思吗?

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


那不是属性。它是元素的内容。

10-06 00:54