3

I want to add a root elements --testsuites into my existing xml report. My current report looks like this

<?xml version="1.0" encoding="utf-8"?>
 <testsuite name="classname" tests="9" failures="3" errors="6" time="2919" 
  disabled="0" skipped="0">
  <testcase name="Setup1" time="5" classname="classname">
  </testcase>
  <testcase name="Setup2" time="49" classname="classname>
  </testcase>
  <testcase name="Setup23" time="357" classname="classname">
  </testcase>
  </testsuite>

I want it change to

    <?xml version="1.0" encoding="utf-8"?>
    <testsuites>
     <testsuite name="classname" tests="9" failures="3" errors="6" time="2919" disabled="0" skipped="0">
      <testcase name="Setup1" time="5" classname="classname">
      </testcase>
      <testcase name="Setup2" time="49" classname="classname">
      </testcase>
      <testcase name="Setup23" time="357" classname="classname">
      </testcase>
     </testsuite>
    </testsuites>

My current doesn't work on me

XmlDocument report = new XmlDocument();
report.Load(fileOfReport);
XmlElement root = report.CreateElement("root");
root.SetAttribute("testsuites","testsuites");
XmlElement child = report.CreateElement("child");
child.GetElementsByTagName("testsuite");
report.DocumentElement.AppendChild(root);
root.AppendChild(child);        
report.Save(fileOfReport);

Is there anyone can help?

5
  • 2
    Please don't copy XML source code from Internet Explorer. Copy XML source code from the XML files you are handling. Commented May 30, 2017 at 18:49
  • I am new to XML area. Can you explain more on this? Are you saying i should convert the xml source code to a file? Commented May 30, 2017 at 18:57
  • 1
    No, I am saying you should not copy and paste the interactive preview that Internet Explorer displays when you open XML files with it. That's useless because it contains extra characters. Open the XML file in a text editor. Commented May 30, 2017 at 18:58
  • @JiangJiali Don't include + and -. People have to remove that stuff by hand before the XML will parse. It's inconsiderate to expect them to do that for you. Commented May 30, 2017 at 19:01
  • thanks everyone, i change it now. please help on how to add the root element Commented May 30, 2017 at 19:12

1 Answer 1

2

You need to call the CreateElement to create the node and append the required childs . Finally append the Newly created node to the Document .

 XmlDocument report = new XmlDocument();
    report.Load(fileOfReport);
    XmlElement root = report.CreateElement("testsuites");           
    var items = report.GetElementsByTagName("testsuite");
    for (int i = 0; i < items.Count; i++)
    {
        root.AppendChild(items[i]);
    }
    report.AppendChild(root);
    report.SaveAs(fileOfReport);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Gowri. This works for me only after change the last line to save instead of load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.