Extract file paths and arguments with space from XML as different strings

xpath
xml path
xpath cheat sheet
powershell read xml file foreach
xpath examples
xpath tester
xpath functions
xpath multiple elements

I have an XML file which contains some info for starting an application from my exe. Like:

<details>
    C:\Program Files\...\abc.exe -env env -direnv direnv
</details>

I need to split as the first part for the path for exe and the remaining as arguments. But i get only C:\Program when i split by space. IS there another solution to acheive the output as:

string[0] = C:\Program Files...\abc.exe string[1] = -env env -direnv direnv

Edit code for extracting from XML:

XMLNodeLst = GetNodeFromXML().SelectNodes("Path");

if (XMLNodeLst != null)
{
    foreach (XmlNode xmlNode in XMLNodeLst)
    {
        foreach (XmlNode pathNode in xmlNode.ChildNodes)
        {
            Match m = Regex.Match(source, pathNode.Name, RegexOptions.IgnoreCase);
            if (m.Success)
            {
                installPath = pathNode.InnerText.Trim();
                break;
            }
            else
            {
                logs.Error("No corresponding value in settings for the provided source name. Please check again!");
                MessageBox.Show("No corresponding value in settings for the provided source name. Please check again!");
                Environment.Exit(-1);
            }
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Path>
  <details>
    C:\Program Files\...\abc.exe -env env -direnv direnv
  </details>
</Path>

To split:

string[] configs = installPath.Trim().Split(' ');
exePath = configs[0];
exeArgs = installPath.Remove(0, installPath.IndexOf(' ') + 1);

You might split on a positive lookbehind and a positive lookahead to assert what is on the left is .exe followed by a whitespace and on the right is a dash followed by 1+ non whitespace characters:

(?<=\.exe) (?=-\S+)

Regex demo

var installPath = @"C:\Program Files\...\abc.exe -env env -direnv direnv";
string[] configs = Regex.Split(installPath, @"(?<=\.exe) (?=-\S+)");
var exePath = configs[0];
Console.WriteLine(exePath); // C:\Program Files\...\abc.exe

See the C# demo

XML Path Language (XPath), Extract file paths and arguments with space from XML as different strings. xpath xpath functions how to get physical path of file in c# powershell read xml file  Splitting and Manipulating Strings. May 1, 2002. Bob DuCharme. XSLT is a language for manipulating XML documents, and XML documents are text. When you're manipulating text, functions for searching strings and pulling out substrings are indispensable for rearranging documents to create new documents.

Sample of a class which could be used to parse the string:

public class ArgumentPathResult {
    public string Path;
    public List<string> Arguments = new List<string>();
}
// Define other methods and classes here
public static class ArgumentPathParser {
    public static ArgumentPathResult ParsePath(string path) {
        ArgumentPathResult result = new ArgumentPathResult();
        string resultString = "";
        foreach(char c in path){
            resultString += c;
            if(resultString.Contains(".exe")){
                break;
            }
        }
        result.Path = resultString;
        result.Arguments = path.Replace(resultString, "")
            .Split('-')
            .Skip(1)
            .ToList();
        return result;
    }
}

Sample of usage:

string path = @"C:\Program Files\...\abc.exe -env env -direnv direnv";
ArgumentPathResult result = ArgumentPathParser.ParsePath(path);

The result object will contain your Path as one variable, and a list of argument and value pairs "env env" and "direnv direnv"

Select-Xml, There are different types of nodes, including element nodes, attribute nodes and text nodes. A location path selects a set of nodes relative to the context node. The normalize-space function returns the argument string with  Remarks. This method is intended to concatenate individual strings into a single string that represents a file path. However, if an argument other than the first contains a rooted path, any previous path components are ignored, and the returned string begins with that rooted path component.

var splitted = pathNode.InnerText.Trim()
    .Split(new string[] { ".exe " }, StringSplitOptions.None);

var path = splitted[0] + ".exe";
var args = splitted[1];

Path.GetFullPath Method (System.IO), Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be Select-Xml -LiteralPath <String[]> [-XPath] <String> [-Namespace uses the Select-Xml cmdlet to get the MethodName nodes in the Types.ps1xml file. InnerXML property of each object in the $Xml variable, trim the white space​  I want to extract the list of all paths to the various leaves in this document, for instance: foo foo.bar foo.bang foo.bang.@bash foo.bang.foobar Is there a term for this process? Take this a step further: let's say I have an .xsd for a truly monstrously complex XML schema. Is there an easy way to extract all such paths from the .xsd?

Hundreds of useful examples, Parameters. path: String. The file or directory for which to obtain absolute path information. Returns. String. There are three types of resources that can provide your application with strings: String XML resource that provides a single string. String Array XML resource that provides an array of strings. Quantity Strings (Plurals) XML resource that carries different strings for pluralization.

Spaces in file names, functx:camel-case-to-words, Turns a camelCase string into space-separated words functx:distinct-element-names, The distinct names of all elements in an XML functx:if-absent, The first argument if it is not empty, otherwise the second argument Resolves a relative URI and references it, returning an XML document. file one contains many words (single word per line) and file two contains many strings per line . Now I want to Grep every line from file2 which contains file1 word. Could any one help me out ? This is like extracting from file2 matches from file1 !! Very appreciated to the one who give solution here !

iOS 4 Programming Cookbook: Solutions & Examples for iPhone, iPad, , Use an underscore ("_") in place of a space between words if file names have more FOR tokens variables (or parameter names) are global, so in complex scripts you are likely to get extra spaces Files to be submitted are those relevant to Understanding how white space works in XML documents can help keep you  how to extract data between two different strings of text file if text file is stored in list of UI path. As line item is required to be extracted… pllo2ptk May 22, 2018, 6:48am

Comments
  • could you please show your code to read xml and splitting strings?
  • Editted in the question
  • That value wouldn't work in a command shell either due to the space in Progam Files. Any chance of having "C:\Program Files\...\abc.exe" -env... ?
  • Try something like this : string input = @"C:\Program Files\...\abc.exe -env env -direnv direnv"; string filename = input.Substring(input.LastIndexOf(@"\") + 1); string[] parameters = filename.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string programName = parameters[0];