【问题标题】:Change XML node with same name?更改具有相同名称的 XML 节点?
【发布时间】:2013-01-31 04:33:45
【问题描述】:

大家! 我有一个 XML 文件,需要更改节点的值,特别是指示的行。我遇到的问题是,如您所见,有很多节点。

我怎样才能改变这条线?这个 XML 文件可能要大得多,所以我正在寻找一种将不同数量的“launch.file”节点考虑在内的解决方案。

需要设置为 True 的节点将由相应的 NAME 标记标识。因此,如果我输入 ULTII,该块的 DISABLED 节点将设置为 True。如果我输入 Catl,那么该块的 DISABLED 节点将被更改。

<?xml version="1.0" encoding="windows-1252"?>
<SBase.Doc Type="Launch" version="1,0">
  <Descr>Launch</Descr>
  <Filename>run.xml</Filename>
  <Disabled>False</Disabled>
  <Launch.ManualLoad>False</Launch.ManualLoad>
  <Launch.File>
    <Name>Catl</Name>
    <Disabled>False</Disabled>
    <ManualLoad>False</ManualLoad>
    <Path>ft\catl\catl.exe</Path>
  </Launch.File>
  <Launch.File>
    <Disabled>False</Disabled>          <!-- change to True -->
    <ManualLoad>False</ManualLoad>
    <Name>ULTII</Name>
    <Path>F:\ULTII.exe</Path>
    <NewConsole>True</NewConsole>
  </Launch.File>
  <Launch.File>
    <Name>ECA</Name>
    <Disabled>False</Disabled>
    <Path>C:\ECA.exe</Path>
  </Launch.File>
</SBase.Doc>

我正在使用 Visual Studio 2012,您是否需要知道。

感谢任何可以帮助我的人,我真的很感激。

【问题讨论】:

  • 我会将 Name 元素设为属性并根据该键搜索记录。
  • 那么那个特定元素有什么特别之处,这意味着您想将那个设置为禁用而不是前一个? Launch.File 元素的哪些方面是相关的?
  • 感谢您的帮助。基本上,需要设置为 True 的节点将由相应的 NAME 标记来标识。因此,如果我输入 ULTII,该块的 DISABLED 节点将设置为 True。如果我输入 Catl,那么该块的 DISABLED 节点将被更改。

标签: c# xml c#-4.0


【解决方案1】:

这可以通过使用LINQ to XML 来实现(参见XDocument Class)。 假设有 single Launch.File 元素和 Name 元素,其值为 "ULTII"

var document = XDocument.Load(...);

var ultiiElement = document
    .Descendants("Launch.File")
    .Single(fileElement => fileElement.Element("Name").Value == "ULTII");
ultiiElement.Element("Disabled").Value = "True"; // or true.ToString()

document.Save(...);

【讨论】:

  • NAME 元素将始终是唯一的,但需要更改其禁用标记的名称将在运行时确定,因此我无法对其进行硬编码。我对 XML 很陌生,所以我会试一试。谢谢!
  • @user2074700,好的,如果它适合你,请考虑接受答案。
【解决方案2】:

这是我做你想做的事的方法

private void DisableLaunchFile(string xmlfile, string launchFileName){
  XDocument doc = XDocument.Load(xmlfile);

  var launchFileElement = doc.Descendants("Launch.File").Where (d => d.Element("Name").Value == lauchFileName);

  launchFileElement.Elements("Disabled").First().Value = true.ToString();

  doc.Save(xmlfile);
}

像这样使用它:

string pathToXmlFile = //assign ;

DisableLaunchFile(pathToXmlFile, "Catl");

DisableLaunchFile(pathToXmlFile, "ULTII");

【讨论】:

    【解决方案3】:

    这个方法可以解决问题:

    public void ChangeNode(string name, string filePath)
    {
    
        XDocument xDocument;
        using (var streamReader = new StreamReader(filePath))
        {
            xDocument = XDocument.Parse(streamReader.ReadToEnd());
        }
    
        var nodes = xDocument.Descendants("Launch.File");
    
        foreach (var node in nodes)
        {
            var nameNode = node.Descendants("Name").FirstOrDefault();
    
            if (nameNode != null && nameNode.Value == name)
            {
                var disabledNode = node.Descendants("Disabled").FirstOrDefault();
    
                if (disabledNode != null)
                {
                    disabledNode.SetValue("True");
                }
            }
        }
    
        using (var streamWriter = new StreamWriter(filePath))
        {
            xDocument.Save(streamWriter);               
        }
    }
    

    你要传入的名字是你要改变的节点的名字,path是xml文件的文件路径。所以你可以这样称呼它:

    ChangeNode("ULTII", "C:\\output.xml");
    

    您可能需要整理一下,就像匹配节点名称不变量大小写或文化,但它应该让你开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      相关资源
      最近更新 更多