Summoner

Hi all,

when I try to load a string from an Xml file, such as "This is a test\nSecond line test"
The "\n" becomes "\\n" which makes my program display incorrectly
Do I need to do something to replace the "\\" , or any other better ways
Thank you


Re: Visual C# General How to load the text " " in XML

Peter Ritchie

Are you sure If you're viewing it in the debugger it replaces all '\' with "\\". Do you get the same thing in the Output Window if you use Trace.WriteLine to display the text




Re: Visual C# General How to load the text " " in XML

mwalts

Well, white space is preserved in XML... so if you have written the entry as
This is a test
Second line test

it should remain two lines when you receive it.

http://www.w3schools.com/xml/xml_syntax.asp

If not, you can always do a stringName = stringName.Replace("\\n","\n");

-mwalts




Re: Visual C# General How to load the text " " in XML

Summoner

Thank you for your reply

public static string GetAttribute(XmlNode n, string strAttr)
{
XmlAttribute attr = n.Attributes.GetNamedItem(strAttr) as XmlAttribute;
if (attr != null)
{
System.Diagnostics.Trace.WriteLine(attr.Value);
return attr.Value;
}
return "";
}

This is my code to retrieved an attribute.
And I use the following code to load the Xml....

XmlDocument xd = new XmlDocument();
xd.PreserveWhitespace = true;
xd.Load(file);

The test in the Xml is "This is a test\nSecond line test".
When I trace it, the debugger shows attr.Value = "This is a test\\nSecond line test"
and the output window shows "This is a test\nSecond line test"
But I want something like:
This is a test
Second line test

Yes, I have tried to replaced the string "\\n" to "\n" and this problem is solved. But somehow I think I have done something wrong and there should be some simple setting to get the data correctly. I am not fimilar in Xml indeed....




Re: Visual C# General How to load the text " " in XML

Peter Ritchie

"\n" is a C#/C++ syntax, and is meaningless in XML. Use the Enter key in XML...