How to decode whitespace in XML using c#

I am trying to iterate through an XML document and output the attribute values to a textbox. It almost works fine, but I cannot get it to convert the encoded white space characters to the real thing.
The output looks like:
Successful Network Logon:

I have unsuccessfully tried to google it. There should be some easy way of doing it, but I can't find it. Ideas anyone?

I am using C# Visual Studio 2010 .

XmlTextReader reader = new XmlTextReader(textBoxFileName.Text);
while (reader.Read())
    if (reader.HasAttributes)
        for (int i = 0; i < reader.AttributeCount; i++)
            richTextBox1.Text = richTextBox1.Text + reader.Name + "=" + reader.Value + Environment.NewLine;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you post some sample source that causes the problem?
Also, do you want to preserve whitespace (newline, tab, etc) or just ignore it?
happygaijinAuthor Commented:
The source code is listed above. Below is pasted an XML element containing white space and escape sequences. I want them preserved, that is I want the xA to be interpreted as new line and xD as cariagge return.

<Row EventTime="2009-10-22T08:12:00" EventID="850" EventSource="Security" EventComputer="" EventUser="NT AUTHORITY\SYSTEM" EventType="8" EventDescription="&amp;#xD;&amp;#xA;A port was listed as an exception when the Windows Firewall started. &amp;#xD;&amp;#xA; &amp;#xD;&amp;#xA;Policy origin: Local Policy &amp;#xD;&amp;#xA;Profile used: Domain &amp;#xD;&amp;#xA;Interface: All interfaces &amp;#xD;&amp;#xA;Name: Legato_NetWorker_Agent &amp;#xD;&amp;#xA;Port number: 8031 &amp;#xD;&amp;#xA;Protocol: TCP &amp;#xD;&amp;#xA;State: Enabled &amp;#xD;&amp;#xA;Scope:"/>
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

I believe that the problem is in the encoding of the white space.
- &amp; is decoded as a stand-alone ampersand rather than a part of the whitespace character definition.
Try using  &#xD;&#xA; in the attribute value instead of "&amp;#xD;&amp;#xA;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
happygaijinAuthor Commented:
The XML is generated by sql server so I cannot change how white space is encoded. But I think that you are right. I'll just have to handle the white space conversion myself.
If you can get the xml as a string, S, you can do something like this:
- freely tranlated from the VB, without test, so no guarantees :)

        var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(S.Replace("&amp;#", "&#")));
        var xRdr = Xml.XmlReader.Create(ms);
        var x = XElement.Load(xRdr);

Then use linq to iterate the attributes:
        var result = element.Attributes.ToList.Select(a => String.Format("Name '{0}' Value = '{1}'", a.Name, a.Value)).ToArray();
... woops, that last line ahould have been:

var result = x.Attributes.ToList.Select(a => String.Format("Name '{0}' Value = '{1}'", a.Name, a.Value)).ToArray();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.