简体   繁体   中英

Adding child element with attributes to XML using System.XML

I have the following XML file:

<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>

And the following code to update the 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");

Using this code I get the following output of the 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>

I want to add the testcase element as a child of testconfiguration . The output should be like:

<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>

How can I add the testcase element as a child of testconfiguration ?

Update The id is now correctly set but the element is not added as a child of testconfiguration node.

you need XmlElement.SetAttribute to add an attribute to the element, setting value to XmlElement.InnerText replaces the nested content.

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

Alternatively you could use Linq to Xml as well.

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") ));
}

Check this Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM