Link to home
Start Free TrialLog in
Avatar of Member_2_4833272
Member_2_4833272

asked on

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:
Logon/Logoff
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++)
        {
            reader.MoveToAttribute(i);
            richTextBox1.Text = richTextBox1.Text + reader.Name + "=" + reader.Value + Environment.NewLine;
        }
        reader.MoveToElement();
    }
}

Open in new window

Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you post some sample source that causes the problem?
Also, do you want to preserve whitespace (newline, tab, etc) or just ignore it?
Avatar of Member_2_4833272
Member_2_4833272

ASKER

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="172.21.10.151" 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: 10.221.11.111/255.255.255.255"/>
ASKER CERTIFIED SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();