0

I want to add a child elements below root elements but to be the first child elements. My current xml is like this

<?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="Setup3" time="357" classname="classname">
  </testcase>
 </testsuite>
</testsuites>

After add the element , i want it look like this

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
 <properties name ="namedata" value="valuedata">
 </properties>
  <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="Setup3" time="357" classname="classname">
   </testcase>
  </testsuite>
</testsuites>

What i did is

XmlDocument report = new XmlDocument();
report.Load(fileOfReport);
XmlElement root = report.CreateElement("properties");
root.SetAttribute("property name", "namedata");
root.SetAttribute("value","valuedata");
var items = report.GetElementsByTagName("testsuite");
for(int i=0; i<items.Count; i++){
    child.AppendChild(items[i]);
}
report.AppendChild(root);
report.Save(fileOfReport);

The code shows properties as a root elements which include testsuite, but actually i want it as a child elemetns which is parallel with testsuite. How should i re-structure that? Note, i can only use xmlDocument not xDocument

Thanks

5
  • Attribute name can't contain a space. That's what 0x20 is. Change property name to propertyName and it should work just fine. Commented May 30, 2017 at 23:06
  • First validate the XML you're using. And then add the new child to it. Commented May 30, 2017 at 23:07
  • Thanks @MarcinJuraszek. Your answer is right. I correct it. But i have another issue. I update the question already. Thank you very much Commented May 30, 2017 at 23:27
  • 1
    <propertyname ="namedata" value="valuedata"> is not xml. Commented May 31, 2017 at 0:15
  • @AlexanderPetrov thanks for your advice. I correct it. Sorry, i am still new to XML area. Commented May 31, 2017 at 18:55

1 Answer 1

1

XmlDocument has plenty of useful methods. In this case, the PrependChild is convenient.

XmlDocument report = new XmlDocument();
report.Load(fileOfReport);

XmlElement root = report.CreateElement("properties");
root.SetAttribute("propertyname", "namedata");
root.SetAttribute("value", "valuedata");

report.DocumentElement.PrependChild(root);

report.Save(fileOfReport);
Sign up to request clarification or add additional context in comments.

3 Comments

Looks like PrependChild is the right methods to call. But i run your solution, it is still missing <properties> </properties> in the xml. it only shows <properties value property name>
@JiangJiali - I just copied your code creating the element. Show valid xml, so I can write the code of its creation. Your xml in the question is incorrect.
Please look the second section of my xml file, that is what I want.

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.