我想添加一个根元素 --testsuites 到我现有的 xml 报告中。 我当前的报告看起来像这样
<?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>
我想把它改成
<?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>
我的电流对我不起作用
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);
有没有人可以帮忙?
最佳答案
您需要调用 CreateElement 来创建节点并附加所需的子节点。最后将新创建的节点附加到 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);
关于c# - 使用 XmlDocument C# 在 xml 中添加根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44269726/