Reputation: 3153
I have this issue wherein whenever I save the xml attribute value which contains '\t' (example: MyNetwork\trash), I get "MyNetwork rash". Also, there are some wherein the '\n' in the text get changed to a hexadecimal value.
Below is the code that I am using.
Config mmsConfig = new Config
{
Path = "MyNetwork\trash"
};
XmlDocument xml = new XmlDocument();
xml.Load(configFile);
XmlNodeList xmlNodeList = xml.SelectNodes("MMS");
foreach (XmlNode node in
from XmlNode xmlNode in xmlNodes
from XmlNode node in xmlNode.ChildNodes
select node)
{
if (node.Attributes == null)
{
throw new ArgumentNullException(configFile, "Unable to locate attributes");
}
if (node.Attributes["path"] != null)
{
node.Attributes["path"].Value = @config.Path;
}
}
XmlTextWriter xmlText = new XmlTextWriter(configFile, Encoding.UTF8);
xmlText.Formatting = Formatting.None;
xml.Save(xmlText);
xmlText.Close();
Please help me in this.
Upvotes: 0
Views: 239
Reputation: 4722
Escape the \
, otherwise \t
will be interpreted as a special character,
Config mmsConfig = new Config
{
Path = "MyNetwork\\trash"
^
};
Check out the Instantiating a String object section in String Class on MSDN:
Note that in C#, because the backslash (
\
) is an escape character, literal backslashes in a string must be escaped or the entire string must be @-quoted.
Upvotes: 1
Reputation: 18655
\t
and \n
are escape sequences so you need to escape their \
to \\t
or \\n
Upvotes: 0
Reputation: 135
\t converts to tab(6 spaces)
\n converts to new line
You can use a @ symbol before to escape these.
string p = @"MyNetwork\trash";
Upvotes: 0
Reputation: 197
The problem is that \t in a string creates a tab character (similar to how \n creates a new line, \" creates a speech mark and \\ creates a backwardslash). Remember, whenever you put a \ in a string, it needs to be doubled up. Replace the \t with \\t and it should work.
Alternatively, put an @ before the string e.g. @"MyNetwork\Trash". Note that if an @ appears before the string and you want to put a " in the string, you need to use "" e.g. @"A speechmark looks like this: ""."
Upvotes: 0