Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class XMLWriteRead
- {
- string xml_path;
- XmlDocument xml = new XmlDocument();
- public XMLWriteRead(string xml_path)
- {
- this.xml_path = xml_path;
- }
- public void XMLWrite(ConnectionInformation connection_info)
- {
- xml.Load(xml_path);
- XmlElement elm = xml.CreateElement("Connection");
- XmlNode node_sourceserver = xml.CreateNode(XmlNodeType.Element, "SourceServer", null);
- node_sourceserver.InnerText = connection_info.source_database.ServerName;
- XmlNode node_targetserver = xml.CreateNode(XmlNodeType.Element, "TargetServer", null);
- node_targetserver.InnerText = connection_info.target_database.ServerName;
- XmlNode node_sourcecatalog = xml.CreateNode(XmlNodeType.Element, "SourceInitialCatalog", null);
- node_sourcecatalog.InnerText = connection_info.source_database.InitialCatalog;
- XmlNode node_targetcatalog = xml.CreateNode(XmlNodeType.Element, "TargetInitialCatalog", null);
- node_targetcatalog.InnerText = connection_info.target_database.InitialCatalog;
- XmlNode node_sourceusername = xml.CreateNode(XmlNodeType.Element, "SourceUsername", null);
- node_sourceusername.InnerText = connection_info.source_database.Username;
- XmlNode node_targetusername = xml.CreateNode(XmlNodeType.Element, "TargetUsername", null);
- node_targetusername.InnerText = connection_info.target_database.Username;
- XmlNode node_sourcepassword = xml.CreateNode(XmlNodeType.Element, "SourcePassword", null);
- node_sourcepassword.InnerText = connection_info.source_database.Password;
- XmlNode node_targetpassword = xml.CreateNode(XmlNodeType.Element, "TargetPassword", null);
- node_targetpassword.InnerText = connection_info.target_database.Password;
- XmlAttribute att_connection_name = xml.CreateAttribute("Name");
- att_connection_name.InnerText = connection_info.ConnectionName;
- elm.AppendChild(node_sourceserver);
- elm.AppendChild(node_targetserver);
- elm.AppendChild(node_sourcecatalog);
- elm.AppendChild(node_targetcatalog);
- elm.AppendChild(node_sourceusername);
- elm.AppendChild(node_targetusername);
- elm.AppendChild(node_sourcepassword);
- elm.AppendChild(node_targetpassword);
- elm.Attributes.Append(att_connection_name);
- xml.DocumentElement.AppendChild(elm);
- xml.Save(xml_path);
- }
- public void XMLCreate()
- {
- XmlDocument xml = new XmlDocument();
- xml.LoadXml("<Connections></Connections>");
- xml.PreserveWhitespace = true;
- xml.Save("connection_list.xml");
- }
- public List<ConnectionInformation> XMLRead()
- {
- XmlDocument doc = new XmlDocument();
- List<ConnectionInformation> connections = new List<ConnectionInformation>();
- doc.Load(xml_path);
- XmlNodeList nodelist = doc.SelectNodes("Connections/Connection");
- foreach (XmlElement elm in nodelist)
- {
- ConnectionInformation temp_connection = new ConnectionInformation();
- temp_connection.ConnectionName = elm.Attributes["Name"].Value;
- temp_connection.source_database.ServerName = elm.SelectSingleNode("SourceServer").InnerText;
- temp_connection.target_database.ServerName = elm.SelectSingleNode("TargetServer").InnerText;
- temp_connection.source_database.InitialCatalog = elm.SelectSingleNode("SourceInitialCatalog").InnerText;
- temp_connection.target_database.InitialCatalog = elm.SelectSingleNode("TargetInitialCatalog").InnerText;
- temp_connection.source_database.Username = elm.SelectSingleNode("SourceUsername").InnerText;
- temp_connection.target_database.Username = elm.SelectSingleNode("TargetUsername").InnerText;
- temp_connection.source_database.Password = elm.SelectSingleNode("SourcePassword").InnerText;
- temp_connection.target_database.Password = elm.SelectSingleNode("TargetPassword").InnerText;
- connections.Add(temp_connection);
- }
- return connections;
- }
- public void XMLUpdate(ConnectionInformation connection)
- {
- XMLDelete(connection.ConnectionName);
- XMLWrite(connection);
- }
- public void XMLDelete(string connection_name)
- {
- xml.Load(xml_path);
- string del_node = string.Format("Connections/Connection[@Name='{0}']", connection_name);
- XmlNode delete = xml.SelectSingleNode(del_node);
- xml.DocumentElement.RemoveChild(delete);
- xml.Save(xml_path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement