0

First things first,I am very new to C# and programming all around. I have a standalone program that will read XML files in a certain location and convert them to plaintext files.

And I have a windows forms app that has a file directory and will display the chosen file in a textbox.

{
        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if ((myStream = openFileDialog1.OpenFile())!= null)
            {
                string strfilename = openFileDialog1.FileName;
                string filetext = File.ReadAllText(strfilename);
                textBox1.Text = filetext;
            }
        }
    }

Below is a snippet of my conversion program.

string[] files = Directory.GetFiles("C:\\articles");
        foreach (string file in files)
        {
            List<string> translatedLines = new List<string>();
            string[] lines = File.ReadAllLines(file);
            foreach(string line in lines)
            {
                if (line.Contains("\"check\""))
                {
                    string pattern = "<[^>]+>";
                    string replacement = " ";
                    Regex rgx = new Regex(pattern);
                    string result = rgx.Replace(line, replacement);
                    translatedLines.Add(result);
                }
            }

How would I modify my program to take the input from the textbox and then perform its conversion? (And Yes I'm aware I have to combine the two programs.)

Kirikaz
  • 1
  • 3
  • You need to write the code of standalone program in to the windows app code. And change it to read only one file instead of all the files from a directory. – Chetan Feb 19 '19 at 03:40
  • Yes I'm aware I have to move my program into the forms app, the question is how to specifically modify my code to get the input from the textbox. – Kirikaz Feb 19 '19 at 06:33
  • 1
    When you move to form app add a textbox on it and suppose it have id as `textbox1` then in your code just do `textbox1.Text` and that will give you textbox value to you. You have to actually do it and try it then see if any issues happen. – Mahesh Feb 19 '19 at 06:38
  • It seems you are reading an XML file by stripping off the `< >`. Know that there is for instance an XDocument class specifically designed to handle XML data. – Hans Kesting Feb 19 '19 at 07:46
  • inside that conversion foreach you have code dealing with a single file. Move that to a separate method, with that "file" as parameter. Then copy that method to your filedialog application and call it with the result of that dialog – Hans Kesting Feb 19 '19 at 10:00

1 Answers1

0

Use XDocument class to parse the XML formatted string to XML Document so that you can get values on each Nodes of the XML

XDocument xDoc = new XDocument(); 
xDoc = XDocument.Parse(filetext);

Now read the content:

var textValue = xDoc.Descendants("Response").First().Attribute("Status").Value;
  • I know 100% im doing this wrong, can you tell me what exactly I got wrong? https://ibb.co/mqpXnKT – Kirikaz Feb 19 '19 at 20:16
  • the Descendants and Attribute value must exist on the XML file you're trying to read. refer to https://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth – john carlo manuel Feb 20 '19 at 05:56