使用C#修改xml文件

我具有如下XML文件内容
我需要修改第4行:

701.50, 24.0 至701.50, 30.0

我该怎么做?

<CableLossConfig>
    <Std Val="CATM1">
        <Path Val="TX1">
            <Loss>701.50,24.0</Loss>
            <Loss>710.50,24.0</Loss>
            <Loss>713.50,24.0</Loss>
            <Loss>779.50,23.0</Loss>
            <Loss>782.00,23.0</Loss>
            <Loss>784.50,23.0</Loss>
            <Loss>826.50,30.0</Loss>
            <Loss>836.50,30.0</Loss>
            <Loss>846.50,30.0</Loss>
            <Loss>1712.50,37.0</Loss>
            <Loss>1732.50,37.0</Loss>
            <Loss>1752.50,37.0</Loss>
            <Loss>1852.50,37.0</Loss>
            <Loss>1880.00,37.0</Loss>
            <Loss>1907.50,37.0</Loss>
        </Path>
    </Std>
</CableLossConfig>
zc88888 回答:使用C#修改xml文件

如果您想要一个不需要创建代表xml文件结构的C#模型的简单解决方案,我建议您使用XDocument .net类(https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument?view=netframework-4.8

我在这里也发现了一个关于stackoverflow的很好的例子,它提出了类似的问题:Edit specific Element in XDocument

我希望这会有所帮助!

,

使用xml linq:

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 tx1 = doc.Descendants("Path").Where(x => (string)x.Attribute("Val") == "TX1").FirstOrDefault();
            List<XElement> losses = tx1.Elements("Loss").ToList();
            XElement loss1 = losses[1];
            loss1.SetValue("701.50,30.0");
        }
    }
}
本文链接:https://www.f2er.com/3139806.html

大家都在问