简体   繁体   English

修改.XML文件中的两个字段

[英]Modifying two fields in .XML file

I have a XML file which looks like:我有一个 XML 文件,它看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<UFCC xmlns="http://actel.com/sweng/afi">
  <Region name="Region_0_0">
    <content>
     <static_data>
     <fixed>
        <value>11111111111111111111111111111111</value>
        <type>HEX</type>
      </fixed>
      </static_data>
  </content>
    <region_start_word>0</region_start_word>
    <number_of_word>16</number_of_word>
    <simulation_value>
      <value>11111111111111111111111111111111</value>
      <type>HEX</type>
    </simulation_value>
  </Region>
  <Region name="Region_1_0">
    <content>
      <static_data>
      <fixed>
        <value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
        <type>HEX</type>
      </fixed>
      </static_data>
    </content>
      <region_start_word>16</region_start_word>
      <number_of_word>16</number_of_word>
    <simulation_value>
      <value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
      <type>HEX</type>
    </simulation_value>
  </Region>
  <Program_Pages/>
</UFCC>

I'm trying to change value fields "11111111111111111111111111111111" to "A...A".我正在尝试将值字段“11111111111111111111111111111111”更改为“A...A”。 I have tried this with Xml.Linq and using examples found online, from which none seem to work.我已经尝试过使用 Xml.Linq 并使用在线找到的示例,但似乎没有一个有效。

How can I modify these fields?如何修改这些字段?

Sorry about the formatting, I'm not that familiar with proper XML formatting and the source file is written in single line.对格式感到抱歉,我对正确的 XML 格式不太熟悉,源文件是单行编写的。

You need to use the namespace:您需要使用命名空间:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            List<XElement> values1s = doc.Descendants(ns + "value").Where(x => (string)x == "11111111111111111111111111111111").ToList();

            foreach (XElement x in values1s)
            {
                x.SetValue(((string)x).Replace("1", "A"));
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM