编辑XML Doc每当有输入进行更改时

时间:2016-06-20 13:03:45

标签: c# xml readxml

public void ModifyXML(string inputAsset, string test, string version)
    {
      File.Create(Constants.XMLDoc).Close();
      XmlReader xReader = XmlReader.Create(xmlDoc);
      while (!xReader.EOF)
      {
        if (xReader.Name != "Asset")
        {
          xReader.ReadToFollowing("Asset");
        }

        //If we have not reached the end of the file
        if (!xReader.EOF)
        {
          XElement asset = (XElement)XElement.ReadFrom(xReader);
          string branchName = (string)asset.Attribute("Name");

          if (branchName == inputAsset)
          {

          }

        }
      }
    }

大家好,所以我目前正在尝试编辑XML文档,而我的输入在我的主文件中并非null。我的XML看起来像这样:

<Properties>
 <Assets>

 <Asset Name="" Version="">
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" />  
  </Asset>

<Asset Name="" Version="">
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" />  
  </Asset>
</Assets>
</Properties>

因此,可能的编辑类型就像是当前测试用例的更改,例如版本值或子版本或名称,甚至可以向资产添加新的测试用例,或者在需要时添加全新的资产。我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用我之前发布的帖子。 xml输入有多大?小xml的答案与大xml不同。您打算使用哪些输入进行修改。

public void ConvertToDirectoryTree()
        {
            XmlReader xReader = XmlReader.Create(xmlDoc);

            while (!xReader.EOF)
            {
                if (xReader.Name != "Asset")
                {
                    xReader.ReadToFollowing("Asset");
                }

                if (!xReader.EOF)
                {
                    XElement asset = (XElement)XElement.ReadFrom(xReader);
                    foreach (XElement testCase in asset.Elements("TestCase"))
                    {

                        //We check if the asset is a main branch folder
                        if (IsMainBranch((string)asset.Attribute("Name") + (string)asset.Attribute("Version")))
                        {
                            //If the folder exists already then add it inside this folder
                            if (Directory.Exists(root + (string)asset.Attribute("Name")))
                            {
                               Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                            //Else we need to create the folder and then add it inside this folder
                            else
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name"));
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                        }
                        //If it is not a main branch folder then we need to handle the name differently
                        else
                        {
                            //If the folder exists already then add it inside this folder
                            if (Directory.Exists(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version")))
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                            //Else we need to create the folder and then add it inside this folder
                            else
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + ((string)asset.Attribute("Version")).Replace(".", "_"));
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }

                        }
                    }
                }
            }
        }