C#.NET Fetch Tab Character from XML Attribute
All,
Process: map Flat File to Application Object. Meta Data: stored in Xml file. Contains field name, delimter, and order number each stored in an attribute. Issue: When fetching the delimiter attribute in Application code, the Tab Character (\t) comes through like this:\\t
(double backslash t).
Xml Sample:
<field name= "Field1" delimiter='\t' orderNum="1"/>
Code Sample:
attrVal = node.Attributes["delimiter"].Value;
Issue: I'm trying to split each line in the file on the delimiter. Something like:
string [] delim = new string[] { attrVal };
string line = streamReader.ReadLine();
string[] record = line.Split(delim, StringSplitOptions.None);
However, because the delimiter is \\t
(double backslash t), the Split function doesn't recognize it.
Questions: Should I 开发者_如何学Cstrip off that first backslash using substring (is that even possible since it's an escape)? Is there a different way I should be accessing the attribute in the Xml file so it won't escape the first backslash? Any info would be appreciated. Thanks in advance.
Joe
You can try using Regex.Unescape(string).
String str = "string \\nstring";
Console.WriteLine(str);
Prints:
string \nstring
and
String str = "string \\nstring";
Console.WriteLine(Regex.Unescape(str));
Prints:
string
string
<field name= "Field1" delimiter='\t' orderNum="1"/>
This is interpreted as the two characters \
and t
and not as tab - that is your problem.
You can encode tab in XML the following way:
<field name= "Field1" delimiter="	" orderNum="1"/>
精彩评论