繁体   English   中英

更新目录中的 XML 文件

[英]Updating XML Files inside a directory

我仍然是 C# 的新手,但我创建了一个实用程序,如果出于某种原因他们的 IP 地址要更改或他们想要更改它,则存储的 IP 地址将更新为当前 IP 地址。

我的代码应该过滤目录中的所有 xml 配置文件并使用新的 IP 地址更新它。 但是,它们是某些文件夹中的一些 xml 配置文件,我不想像说 net.tcp://localhost 那样更改它们。

<endpoint name="SessionLocal" address="net.tcp://localhost:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint name="DataLocal" address="net.tcp://localhost:7732/DataServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.IDataService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>

接下来的几个文件夹包含我想要更改的 xml 文件,它们具有实际的 IP 地址,例如

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

1) 我将如何添加代码,以便用户可以选择手动输入 IP 地址,以更新 xml 配置文件中的 Ipaddress。 (如果 xml 配置文件中的端点地址没有 localhost 地址)

2) 如何在 else 语句中编写代码以继续目录中的枚举?

在此处输入图片说明

        static void Main(string[] args)
    {
        var dir = Directory.EnumerateDirectories(@"C:\Program Files (x86)\Stuff\Noodles");
        foreach (var folder in dir)
        {
            var path = Directory.EnumerateFiles(folder, "*.config", SearchOption.AllDirectories);
            string newIPAddress = "10.246.31.4";
            foreach (var xmlfile in path)
            {
                var doc = XDocument.Load(xmlfile);
                var endpointsToUpdate = doc
                    .Descendants("endpoint")
                    .Where(x => new Uri((string)x.Attribute("address")).Host != "localhost")
                    .ToArray();

                // skip if there is nothing to update
                //if (!endpointsToUpdate.Any()) return;
                if (endpointsToUpdate.Any());
                {

                    foreach (var endpoint in endpointsToUpdate)
                    {
                        string address = (string)endpoint.Attribute("address");
                        string pattern = "//\\d[^:]+";
                        address = Regex.Replace(address, pattern, "//" + newIPAddress);

                        endpoint.Attribute("address").SetValue(address);
                    }
                    else 
                    {

                    }

                    doc.Save(xmlfile);
                } 
            }
        }
    }
}

}

如果您使用正则表达式只匹配不包含特定字符串(即 localhost)的字符串怎么办? 因此,如果字符串不包含 localhost,请执行替换,否则跳过替换。 反之亦然,无论您选择如何看待它。

^(?!. DontMatchThis)。 $

只需将模式的第一个字符更改为 \\d (数字)即可解决问题:

           string newIP = "10.36.31.4";

            string addr1 = "net.tcp://localhost:7732/AuthenticationServices/Secure";
            string addr2 = "net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure";

            string pattern = "//\\d[^:]+";

            string newaddr1 = Regex.Replace(addr1, pattern, "//" + newIP);
            string newaddr2 = Regex.Replace(addr2, pattern, "//" + newIP); 

暂无
暂无

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

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