简体   繁体   中英

Updating XML Files inside a directory

I'm still kind of new to C# but I created a utility where if for whatever reason their IP address were to change or they want to change it, the IP address that was stored will update to the current IP address.

My code is supposed to be filtering all the xml config files within the directory and updating it with a new IP address. However their are some xml config files in certain folders that I DON'T want to change like the ones that say 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>

The next couple folders contain xml files that I WANT to change have actual IP addresses like

<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) How would I add code so that user will have the option to either manually type in an IPaddress that will update the Ipaddress within the the xml config file. (IF the endpoint address within xml config file does not have an address of localhost)

2) How would I write code inside the else statement to continue the enumeration within the directory?

在此处输入图片说明

        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);
                } 
            }
        }
    }
}

}

What if you use a regex to only match strings that don't contain a certain string (ie localhost)? So if the string doesn't contain localhost do the Replace, otherwise skip the Replace. Or vice-versa, however you choose to look at it.

^(?!. DontMatchThis). $

Simply changing the first character of the pattern to \\d (a digit) will solve issue :

           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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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