0

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.

sarbo
  • 1,661
  • 6
  • 21
  • 26

1 Answers1

2

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

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • The `id` is now correctly set but the element is not added as a child of `testconfiguration`. Thanks for your suggestion. – sarbo Aug 10 '16 at 09:24