score:2

Accepted answer

LINQ expressions shouldn't normally modify the value of the source. They should only return new values (think them as a filter... You put something in, something different comes out)

foreach (var desc in doc.Descendants()) {
    var nodes = desc.Nodes().Where(p => p.NodeType == XmlNodeType.Text);

    foreach (XText node in nodes) {
        node.Value = node.Value.ToUpper();
    }
}

score:1

I solved it this way - not sure its the best but it works.

private void btnConvert_Click(object sender, EventArgs e)
    {
        XDocument doc = XDocument.Parse(txtXml.Text);
        ProcessElement(doc.Elements().First());
        txtXml.Text = doc.ToString(SaveOptions.DisableFormatting);
    }

    private void ProcessElement(XElement element)
    {
        if (element.HasElements)
            element.Elements().ToList().ForEach(e => ProcessElement(e));
        else
            element.Value = element.Value.ToUpper();
    }

Related Articles