There are two files: The txt file (source file), in which 4 columns and several lines enter image description here

Another file (the file in which you need to update the lines), xml format. Here is the part of the xml code of the file you want to replace with the data from the textbox enter image description here

The point is to replace:
ConnectionManagaerID = "2nd txt column"
ConnectionManagaerID = "1st txt column"

I can not implement this update.
Tried using an array, creating a datatable.

  • Build a program based on XmlReader -> XmlWriter, for example. Or download xml, upload the xml in a way that was familiar to you, so that the conversation would be more substantive. DataTable is not suitable because it allows you to edit only the "data" section. - nick_n_a
  • You can read the text - and just replace the text. - nick_n_a
  • Ok, DataTable is discarded. How in C # conditionally (key) compares two files? I can not consistently register it. What would you find on the Internet separate solutions to the issue. - Andrey
  • one
    It is possible so: А[i] == B[i] preliminary in And and B having read files. - nick_n_a
  • one
    Try in parts: 1) read the first file 2) read the second file 3) make changes 4) rewrite the file. You now have too many steps for one question. - default locale

1 answer 1

We have such a text file with such content.

Package.ConnectionManagers [xxx] | {XXX-XXXX-XXXX-XXXX-XXXX} | Data Source = YYY; User ID = fjfjf; | ACS Available Moths.dts

We have this XML file

 <?xml version="1.0" encoding="utf-8"?> <connections> <connection refId="PackageId..." connectionManagerID="conManagerId..." connectionManagerRefId="conManRefId..." description="des..." name="OleDbConnection..." /> </connections> 

After we have this file

 <?xml version="1.0" encoding="utf-8"?> <connections> <connection refId="PackageId..." connectionManagerID="{XXX-XXXX-XXXX-XXXX-XXXX}" connectionManagerRefId="Package.ConnectionManagers[xxx]" description="des..." name="OleDbConnection..." /> </connections> 

Here is the complete program code, you can experiment

 class Program { static void Main(string[] args) { string xmlFile = @"D:\cons.xml"; string txtFile = @"D:\cons.txt"; //MakeXML(xmlFile); ChangeConnectionAttributes(xmlFile, txtFile); } private static void MakeXML(string xmlFile) { XDocument doc = new XDocument(); XElement elm = new XElement("connections", new XElement("connection", new XAttribute("refId", "PackageId..."), new XAttribute("connectionManagerID", "conManagerId..."), new XAttribute("connectionManagerRefId", "conManRefId..."), new XAttribute("description", "des..."), new XAttribute("name", "OleDbConnection..."))); doc.Add(elm); doc.Save(xmlFile); } private static void ChangeConnectionAttributes(string xmlFile, string txtFile) { var strs = File .ReadAllText(txtFile) .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); XDocument doc = XDocument.Load(xmlFile); var c1 = doc.Descendants("connection") .First() .Attributes() .First(a => a.Name == "connectionManagerID").Value = strs[1]; var c2 = doc.Descendants("connection") .First() .Attributes() .First(a => a.Name == "connectionManagerRefId").Value = strs[0]; doc.Save(xmlFile); } } 
  • @AlexanderPetrov well, so that worked requests. OK, you can like you. - Bulson pm
  • As I understand it in txtFile and xmlFile, there should be file paths. But I do not work out. - Andrey
  • @Andrey, you need to go through the debugging, and see what's wrong. I wrote the code with real files in the same format as you have in the pictures, the code is checked, it is working. Or attach the actual content in text form, and not in the golimyh pictures, so I can check it myself. - Bulson
  • Checked code worker-updates. Perhaps this is my cant in the description, the update is not based on the necessary logic. - Andrey
  • one
    @Andrey I changed the answer, read carefully. - Bulson 2:46 pm