开发者

C#.NET Fetch Tab Character from XML Attribute

开发者 https://www.devze.com 2023-02-28 07:06 出处:网络
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.

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="&#x9;" orderNum="1"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消