使用 System.XML 将具有属性的子元素添加到 XML

Adding child element with attributes to XML using System.XML

我有以下 XML 文件:

<testsuite name="Tests" rxversion="5.4.5.19886" id="d1203701-d61c-4ae6-932d-faa44beb925a" reportfilename="%S_%Y%M%D_%T.rxlog" reporttemplatefolder="" reportxslfilename="" placescreenshotsinfolder="True" ReportTime="RelativeToTestSuiteStartTime" reportwriteinterval="30000ms" reportcompress="False" enabletracingscreenshots="True" TracingScreenshotMode="Foreground" TracingScreenshotQuality="40" reportlevel="Info;20" warnunboundvariables="False">
<testconfigurations default="TestRun">
    <testconfiguration name="TestRun" />
</testconfigurations>
</testsuite>

和下面的代码来更新XML:

var xe = new XmlDocument();
xe.Load("Z:\Tests\Tests.rxtst");

string testconfig = "//testsuite/testconfigurations/testconfiguration";
string testconfigend = "//testsuite";

XmlNode tc = xe.SelectSingleNode(testconfig);
XmlNode tcend = xe.SelectSingleNode(testconfigend);

XmlElement xs = xe.CreateElement("testcase");
xs.SetAttribute("id", "450c9a87-75dc-4538-bc2c-6df6eb359d2a");
XmlNode par = tc.ParentNode;
par.InsertBefore(xs, tc.LastChild);

xe.Save("st.rxtst");

使用此代码,我得到 xml 的以下输出:

<testsuite name="Tests" rxversion="5.4.5.19886" id="d1203701-d61c-4ae6-932d-faa44beb925a" reportfilename="%S_%Y%M%D_%T.rxlog" reporttemplatefolder="" reportxslfilename="" placescreenshotsinfolder="True" ReportTime="RelativeToTestSuiteStartTime" reportwriteinterval="30000ms" reportcompress="False" enabletracingscreenshots="True" TracingScreenshotMode="Foreground" TracingScreenshotQuality="40" reportlevel="Info;20" warnunboundvariables="False">
    <testconfigurations default="TestRun">
        <testconfiguration name="TestRun" />
        <testcase id="450c9a87-75dc-4538-bc2c-6df6eb359d2a" />
    </testconfigurations>
</testsuite>

我想添加 testcase 元素作为 testconfiguration 的子元素。输出应该是这样的:

<testsuite name="Tests" rxversion="5.4.5.19886" id="d1203701-d61c-4ae6-932d-faa44beb925a" reportfilename="%S_%Y%M%D_%T.rxlog" reporttemplatefolder="" reportxslfilename="" placescreenshotsinfolder="True" ReportTime="RelativeToTestSuiteStartTime" reportwriteinterval="30000ms" reportcompress="False" enabletracingscreenshots="True" TracingScreenshotMode="Foreground" TracingScreenshotQuality="40" reportlevel="Info;20" warnunboundvariables="False">
    <testconfigurations default="TestRun">
        <testconfiguration name="TestRun">
            <testcase id="450c9a87-75dc-4538-bc2c-6df6eb359d2a"/>
        </testconfiguration>
    </testconfigurations>
</testsuite>

如何将 testcase 元素添加为 testconfiguration 的子元素?

更新 id 现在已正确设置,但该元素未添加为 testconfiguration 节点的子节点。

您需要 XmlElement.SetAttribute 为元素添加属性,将值设置为 XmlElement.InnerText 替换嵌套内容。

xs .SetAttribute("id", "450c9a87-75dc-4538-bc2c-6df6eb359d2a");

或者您也可以使用 LinqXml

XDocument doc = XDocument.Parse(input);

foreach(var element in doc.Descendants("testconfiguration"))
{
    element.Add(new XElement("testcase", new XAttribute("id","450c9a87-75dc-4538-bc2c-6df6eb359d2a") ));
}

勾选这个Demo