Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中,在XmlAttribute的开头加上破折号_C#_Xml_Cruisecontrol.net - Fatal编程技术网

C# 在C中,在XmlAttribute的开头加上破折号

C# 在C中,在XmlAttribute的开头加上破折号,c#,xml,cruisecontrol.net,C#,Xml,Cruisecontrol.net,我希望能够做到以下几点: //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

我希望能够做到以下几点:

 //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);
        }
我检查了这个:和其他,但我没有找到一个我可以使用的答案

-编辑-

这是ccnet.config的一个示例:


谢谢

您不能,这不是有效的属性名称。您可以将其存储为属性值—只需使用XML允许的名称即可

我很确定CruiseControl.Net也没有这样做,因为它无法在这样的文件上使用内置解析器

编辑:它的元素值也称为内容。设置如下:

var buildArgs = doc.CreateElement("buildArgs");
buildArgs.Value = "-D:project.rc_file";

你不能,这不是一个有效的属性名。您可以将其存储为属性值—只需使用XML允许的名称即可

我很确定CruiseControl.Net也没有这样做,因为它无法在这样的文件上使用内置解析器

编辑:它的元素值也称为内容。设置如下:

var buildArgs = doc.CreateElement("buildArgs");
buildArgs.Value = "-D:project.rc_file";

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

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

这不是一个属性。它是元素内容。

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

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

这不是一个属性。这是元素含量。

嗯。。。如果你看,你会看到破折号D-D…这个答案也是正确的,因为它不是一个属性,而是一个元素。谢谢。@LolCat关键点不是它是一个元素而不是一个属性,因为名称起始破折号对于元素仍然无效-关键点是它是值,而不是属性name@Marc格雷威尔:是的,你是对的。这没关系,因为它变成了元素的值,而不是元素或属性的名称。XML不允许破折号。谢谢你的说明。嗯。。。如果你看,你会看到破折号D-D…这个答案也是正确的,因为它不是一个属性,而是一个元素。谢谢。@LolCat关键点不是它是一个元素而不是一个属性,因为名称起始破折号对于元素仍然无效-关键点是它是值,而不是属性name@Marc格雷威尔:是的,你是对的。这没关系,因为它变成了元素的值,而不是元素或属性的名称。XML不允许破折号。谢谢你的说明。