Linq Split function Query
I've a csv file that contains the records below:
Col1,Col2,Col3
"Test,Test1",1,3
And I've Linq query below which splits the 开发者_如何学Gocsv to XML document. However, for given example above it's spliting the "Test,Test1" in to two separate element.
var xml = new XElement("Root", source.Select(x =>
new XElement("Testing", x.Split(splitChar).Select((Field, index) =>
new XElement("Field" + index, Field)))).Skip(1));
This generates something like:
<Root>
<Testing Field0="Test" Field01="Test1" Field02="1" Field03="3" />
</Root>
But what i want is:
<Root>
<Testing Field0="Test,Test1" Field01="1" Field02="3" />
</Root>
Could anyone please help me achive above?
Instead of this:
x.Split(splitChar)
try this approach using a regular expression:
Regex.Matches(x, "\"[^\"]*\"|[^,]+").Cast<Match>().Select(m => m.Value)
This matches either a field that is quoted, for example: "foobar,baz"
or something that doesn't contain a comma.
There are some limitations with my answer:
- It doesn't handle escaped quotes inside the string such as in this case:
"Test1,Test\"two\",Test3",0,1
. Do you need this? - The separator is no longer a parameter, but hard coded. You have to be careful as some characters are special and need to be escaped in a regular expression. Consider using
Regex.Escape
if you want to build the regular expression based on the runtime value ofsplitChar
.
精彩评论