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
Succ essful 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 .
The output looks like:
Logon/Logoff
Succ
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();
}
}
Can you post some sample source that causes the problem?
Also, do you want to preserve whitespace (newline, tab, etc) or just ignore it?
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:1 2:00" EventID="850" EventSource="Security" EventComputer="172.21.10.1 51" EventUser="NT AUTHORITY\SYSTEM" EventType="8" EventDescription="&#xD ;&#xA; A port was listed as an exception when the Windows Firewall started. &#xD;&#xA; &#xD;&#xA;Policy origin: Local Policy &#xD;&#xA;Profile used: Domain &#xD;&#xA;Interfac e: All interfaces &#xD;&#xA;Name: Legato_NetWorker_Agent &#xD;&#xA;Port number: 8031 &#xD;&#xA;Protocol : TCP &#xD;&#xA;State: Enabled &#xD;&#xA;Scope: 10.221.11.111/255.255.255. 255"/>
<Row EventTime="2009-10-22T08:1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(Sys tem.Text.E ncoding.UT F8.GetByte s(S.Replac e("&#" , "&#")));
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();
- freely tranlated from the VB, without test, so no guarantees :)
var ms = new System.IO.MemoryStream(Sys
var xRdr = Xml.XmlReader.Create(ms);
var x = XElement.Load(xRdr);
Then use linq to iterate the attributes:
var result = element.Attributes.ToList.
... 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();
var result = x.Attributes.ToList.Select