[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to decode whitespace in XML using c#

Posted on 2011-10-20
7
Medium Priority
?
1,010 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:happygaijin
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 37005227
Can you post some sample source that causes the problem?
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37005244
Also, do you want to preserve whitespace (newline, tab, etc) or just ignore it?
0
 

Author Comment

by:happygaijin
ID: 37005379
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"/>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Accepted Solution

by:
MikeToole earned 2000 total points
ID: 37005723
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;
0
 

Author Comment

by:happygaijin
ID: 37006939
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.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37007282
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();
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37007291
... 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();
 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question