gpt4 book ai didi

c# - 如何在 C# 中编辑和保存 abc.testsettings(xml) 文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:34:39 26 4
gpt4 key购买 nike

我有以下内容的文件。我想删除 DeploymentItem 下的所有元素并创建新的。

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Settings" id="6f67be45-4967-40c3-a89f7665cc7f654e2" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Deployment>
<DeploymentItem filename="TestCaseXml\LTestCases.xml" />
<DeploymentItem filename="..\..\Framework\Common\Xmls\TestSuite.xml" />
<DeploymentItem filename="TestCaseXml\bTestCases.xml" />
<DeploymentItem filename="TestCaseXml\PTestCases.xml" />
<DeploymentItem filename="TestCaseXml\1TestCases.xml" />
</Deployment>
</TestSettings>

当我使用下面的代码时,它会给我下面给出的输出

XmlDocument doc2 = new XmlDocument();
doc2.Load(testBinaryFile);
foreach (XmlNode node in doc2.DocumentElement.ChildNodes)
{
if (node.Name == "Deployment")
{
node.RemoveAll();

foreach(string element in Folders)
{
if(!element.Contains("Scripts"))
{
string[] Files7 = Directory.GetFiles(element);
foreach(string File2 in Files7)
{
string addName = File2.Substring(File2.LastIndexOf(@"TestBinaries\")).Replace("TestBinaries\\", "");
XmlElement elem = doc2.CreateElement("DeploymentItem");
elem.InnerText = "filename='" + addName + "'";
node.AppendChild(elem);
}

}
else
{
string addName = element.Substring(element.LastIndexOf("TestBinaries\\")).Replace("TestBinaries\\","") + "\\";
XmlElement elem = doc2.CreateElement("DeploymentItem");
elem.InnerText = @"filename='" + addName + "'";
node.AppendChild(elem);
}
}

doc2.Save(testBinaryFile);
}



<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Settings" id="6f67be45-4967-40c3-a89f7665cc7f654e2" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Deployment>
<DeploymentItem xmlns="">filename='Scripts\'</DeploymentItem>
<DeploymentItem xmlns="">filename='TestCaseXml\bTestCases.xml'</DeploymentItem>
<DeploymentItem xmlns="">filename='Xmls\TestSuite.xml'</DeploymentItem>
</Deployment>
</TestSettings>

我想要如下所示的输出。请帮忙。

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Settings" id="6f67be45-4967-40c3-a89f7665cc7f654e2" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Deployment>
<DeploymentItem filename='Scripts\' />
<DeploymentItem filename='TestCaseXml\bTestCases.xml' />
<DeploymentItem filename='Xmls\TestSuite.xml' />
</Deployment>
</TestSettings>

最佳答案

关于将 xmlns="" 添加到新元素,那是因为您的 XML 具有默认 namespace 。请注意,在 XML 中,后代元素隐式继承祖先的默认 namespace ,除非另有说明。

也就是说,在您发布的预期输出中,DeploymentItem 位于默认命名空间 “http://microsoft.com/schemas/VisualStudio/TeamTest/2010” 中。

您可以使用 CreateElement() overload它接受前缀、localName、namespaceURI 参数以在命名空间中创建元素。具体来说,尝试替换这一行:

XmlElement elem = doc2.CreateElement("DeploymentItem");

用这个:

XmlElement elem = doc2.CreateElement(
string.Empty,
"DeploymentItem",
"http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
);

关于c# - 如何在 C# 中编辑和保存 abc.testsettings(xml) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com