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

时间:2015-06-09 03:02:25

标签: c# xml

我的文件包含以下内容。我想删除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>

2 个答案:

答案 0 :(得分:2)

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

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

您可以使用CreateElement() overload接受prefix,localName,namespaceURI参数来创建命名空间中的元素。具体而言,请尝试替换此行:

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

这一个:

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

答案 1 :(得分:0)

尝试以下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication33
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<TestSettings name=\"Settings\" id=\"6f67be45-4967-40c3-a89f7665cc7f654e2\" xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\">\n" +
                  "<Description>These are default test settings for a local test run.</Description>\n" +
                  "<Deployment>\n" +
                "<DeploymentItem filename=\"TestCaseXml\\LTestCases.xml\" />\n" +
                "<DeploymentItem filename=\"..\\..\\Framework\\Common\\Xmls\\TestSuite.xml\" />\n" +
                "<DeploymentItem filename=\"TestCaseXml\\bTestCases.xml\" />\n" +
                "<DeploymentItem filename=\"TestCaseXml\\PTestCases.xml\" />\n" +
                "<DeploymentItem filename=\"TestCaseXml\\1TestCases.xml\" />\n" +
                "</Deployment>\n" +
                "</TestSettings>\n";

            string defaultItems =
                "<DeploymentItem xmlns=\"\">filename='Scripts\'</DeploymentItem>\n" +
                "<DeploymentItem xmlns=\"\">filename='TestCaseXml\\TestCases.xml'</DeploymentItem>\n" +
                "<DeploymentItem xmlns=\"\">filename='Xmls\\TestSuite.xml'</DeploymentItem>\n";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(input);
            XmlNode deployment = doc.GetElementsByTagName("Deployment")[0];
            deployment.RemoveAll();
            deployment.InnerXml = defaultItems;


        }

    }
}