Inserting data into xml file

不羁的心 提交于 2021-01-29 18:56:28

问题


I want to update connection string into xml file whenever new customer is registered with the website with its details in asp.net core 3.1

My connection string format is

<ConnectionStrings>
    <add name="myConnectionString1" connectionString="server=localhost;database=myDb1;uid=myUser1;password=myPass;" />
    <add name="myConnectionString2" connectionString="server=localhost;database=myDb2;uid=myUser2;password=myPass;" />
</ConnectionStrings>

I want to achieve this using c# code so on registration this data will automatically updated in xml file


回答1:


Off the top of my head:

You can edit your existing XML as follows.

// Loads XML from file.
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\yourFile.xml");

// Get a list of all <ConnectionStrings> nodes.
XmlNodeList nodes = doc.SelectNodes("/ConnectionStrings");

// Loop through each found node.
foreach (XmlNode node in nodes)
{
   // Search for the 'connectionString' attribute into the node.
   XmlAttribute attrib = node.Attributes["connectionString"];

   if (attrib != null)
   {
      // If the attribute could be found then you might want to get its value.
      string currentValue = attrib.Value;    
      // Or do some stuff with the value. E.g: Set a new value based on the old one.
      attrib.Value = currentValue + "your modification here";
   }
}

// Finally you might want to save the document in other location.
doc.Save(@"C:\yourNewFile.xml";

EDIT: As per requested you can add a new connection string element to your connection strings section as follows. (Not tested. It could require some adjustments)

using System.Configuration;

   // Create a connection string element and add it to
   // the connection strings section.
   private bool CreateConnString(string name, string connString, string uId, string password)
   {
     try
     {
        // Get the application configuration file.
        System.Configuration.Configuration config =
               ConfigurationManager.OpenExeConfiguration(
               ConfigurationUserLevel.None);

        // Get the current connection strings count.
        int connStrCnt = 
           ConfigurationManager.ConnectionStrings.Count;

        // Create a connection string element and
        // save it to the configuration file.

        // In your case 'connString' parameter should be 
        // something like "server=localhost;database=myDb1;"
        ConnectionStringSettings csSettings =
               new ConnectionStringSettings(name, connString +
               "uid=" + uId + ";password=" + password + ";");

        // Get the connection strings section.
        ConnectionStringsSection csSection =
           config.ConnectionStrings;

        // Add the new element.
        csSection.ConnectionStrings.Add(csSettings);

        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified);
        return true;
      }
     catch (Exception e)
     {
        return false;
     }
   }


来源:https://stackoverflow.com/questions/61775396/inserting-data-into-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!