【问题标题】:How to go through xml config file and update every endpoint C#如何通过 xml 配置文件并更新每个端点 C#
【发布时间】:2019-12-12 05:15:00
【问题描述】:

我有一个软件,它在安装时会询问我服务器的 IP 地址,并将该地址存储到不同文件夹中的多个配置文件中。目前,我的代码仅更新单个文件中端点地址的第一个实例,但我试图将文件中的所有端点更新为相同的 IP 地址。

另外,当用户下载软件时,我将如何修改代码以查看软件的安装位置,然后运行更新 IP 地址。

如果有人能告诉我如何更新不同文件夹内配置文件中的多个 IP 地址,那会更酷。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;


namespace ConfigTool
{
    class Class1
    {
        const string FILENAME = @"C:\Program Files (x86)\******\***\***\*****\*****.****.****.exe.config";
        public static IPAddress GetIPAddress(string hostName)
        {
            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

        static void Main(string[] args)
        {
            XDocument doc =  XDocument.Load(FILENAME);
                XElement endpoint = doc.Descendants("endpoint").FirstOrDefault();

                string address = (string)endpoint.Attribute("address");
                string newIp = "10.249.30.4";

                string pattern = "//[^:]+";
                address = Regex.Replace(address, pattern, "//" + newIp);

                endpoint.Attribute("address").SetValue(address);

            doc.Save(FILENAME);
        }
    }
}

XML 文件

<endpoint name="***Local" address="net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.ISubscriptionService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint name="***Local" address="net.tcp://10.243.32.4:7732/EventPublishServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IPublishService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="*******" />
        </identity>
      </endpoint>
      <endpoint name="****" address="net.tcp://10.243.32.4:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

【问题讨论】:

    标签: c# asp.net .net xml


    【解决方案1】:

    一个简单的改变,至少应该解决你最初的问题

       XDocument doc =  XDocument.Load(FILENAME);
       List<XElement> endpoints = doc.Descendants("endpoint").ToList();
       foreach (var endpoint in endpoints) {
            string address = (string)endpoint.Attribute("address");
            string newIp = "10.249.30.4";
    
            string pattern = "//[^:]+";
            address = Regex.Replace(address, pattern, "//" + newIp);
    
            endpoint.Attribute("address").SetValue(address);
        }
    

    要在具有特定类型的文件夹下查找所有特定类型的文件,然后在加载文档时使用,请在此处查看此答案:How to list text files in the selected directory in a listbox?

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 2016-06-21
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      相关资源
      最近更新 更多