Losing new lines with XDocument

I populate TreeView with nodes based on an XML document. However, it seems that when I go to put the attribute value in the text box, it loses its new lines / return / carriage tabs.

I'll start by adding a group of nodes with task names. Each task has one or more queries in an XML document. For instance:<Tasks><Task name="aTaskName"><Queries><add Query="a long string with tabs and newlines and such" /></Queries></Task> ... </Tasks>

void PopulateQueries(XDocument doc, TreeView tree)
{
    foreach (TreeNode node in tree.Nodes)
    {
        var taskName = node.Text;
        var queriesNode = node.Nodes.Add("Queries");
        var queries = doc.Descendants("Tasks")
            .Descendants("Task")
            .Where(d => d.Attribute("name").Value == taskName)
            .Descendants("Queries")
            .Descendants("add")
            .ToList();

        for (int i = 0; i < queries.Count;i++)
        {
            queriesNode.Nodes.Add(queries[i].Attribute("Query").Value, "query" + i);
        }
    }
}

Later in the node click event:

void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    textBoxRaw.Text = string.Empty;
    if (e.Node.Text.StartsWith("query"))
    {
        textBoxRaw.Text = e.Node.Name;
    }
}

The value of the Query attribute contains a long SQL query with newline characters, tabs, etc. But none of this appears in the text box (multi-line), despite all my screams in Visual Studio. What am I doing wrong? Also, var doc = XDocument.Load(filename, LoadOptions.PreserveWhitespace);doesn’t work either.

+5
source share

All Articles