编辑XML文档只要有输入即可对其进行更改 [英] Edit XML Doc Whenever there is a input to change it

查看:81
本文介绍了编辑XML文档只要有输入即可对其进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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文档,而我的输入中的main不为null时.我的XML看起来像这样:

Hello guys so I'm currently trying to edit an XML Doc whenevr my inputs are not null in my main. My XML looks like this:

<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>

因此,可能的编辑类型类似于当前测试用例的更改,例如版本值或子版本或名称,甚至向资产添加新的测试用例,或者根据需要添加全新的资产.我将如何处理?

So the kinds of edits that are possible are like changes of a current test case s ofor example the version value or sub version or name or even adding a new test case to an asset or adding a completely new asset if need be. How would I go about this?

推荐答案

使用我在先前发布中发布的代码.您输入的xml大小是多少?答案将与小xml的大xml不同.您打算将哪些输入用于修改.

Use the code I posted on previous posting. How large is you xml input? The answer would be different fro small xml than large xml. What inputs do you plan to use for the modifications.

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"));
                            }

                        }
                    }
                }
            }
        }

这篇关于编辑XML文档只要有输入即可对其进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆