I already checked several questions posted at Stackoverflow but seems that none of them can solve my situation which i can try to explain.
I have next XML file into disk ( commands.xml
) with following contents:
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category name="Standard">
<cmd name="pwd">
<description>Get working dir.</description>
<code>echo getcwd();</code>
</cmd>
<cmd name="cat">
<description>Reads a file.</description>
<code>$fh = fopen("<<cmd>>", 'r') or die ("Cant open file");
$lines = fread($fh, filesize("<<cmd>>"));
echo "$lines";
fclose($fh);</code>
</cmd>
</category>
</categories>
I'm trying to read the child-node <code>
of <cmd name="cat">
, as you can see it contains several lines.
My actual code is:
var xml = XElement.Load("commands.xml");
var query = xml.Descendants()
.Where(x => x.Name == "category" && x.Attribute("name").Value == "Standard")
.Descendants()
.Where(x => x.Name == "cmd" && x.Attribute("name").Value == "cat");
var first_query_result = query.FirstOrDefault();
if (first_query_result != null)
{
textBox_cmd_code_editor.Text = first_query_result.Descendants("code").Single().Value;
}
The problem is that the text is concatenated and line jumps ( \\r\\n
) are stripped. I need to get the text as is, without any concatenation or reformatting, but don't know how to achieve it.
I tryed multiple options but couldn't find a solution.
Thanks.
I think it may have something to do with the object you're placing the text into (the textBox_cmd_code_editor.Text).
I placed your output into a normal string variable and the newlines were preserved.
Try placing the output into a normal string variable and then manipulating it from there. It's likely your target object just don't support newlines, ect.
Edit: What you need is to convert the linebreaks to Environment.NewLine, as follows:
string originalString;
string convertedString;
if (first_query_result != null)
{
originalString = first_query_result.Descendants("code").Single().Value;
convertedString = Regex.Replace(originalString, @"\r\n?|\n", Environment.NewLine);
textBox1.Text = convertedString;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.