Separating numbers joined by "," into an array
-
I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
}But, inside the pranthesis has an error which says:
cannot convert from 'string[] to string'
an example of the XML content is:
1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872
How can I solve this problem?
-
I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
}But, inside the pranthesis has an error which says:
cannot convert from 'string[] to string'
an example of the XML content is:
1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872
How can I solve this problem?
The
String.Split
method returns an array of strings. And you cannot pass an array toint.Parse
. You first need to capture the array and then convert the numbers individually. Also, you should always useTryParse
to convert strings to numbers, in order to avoid exceptions. -
I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
}But, inside the pranthesis has an error which says:
cannot convert from 'string[] to string'
an example of the XML content is:
1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872
How can I solve this problem?
You put the .Split INSIDE the parameter of the Parse call. Split will return a string[] (string array), not a string. You have to do the split outside of the Parse, then parse each value separately and create your int[] from those values. Parse will only work on individual strings, not arrays of them.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
You put the .Split INSIDE the parameter of the Parse call. Split will return a string[] (string array), not a string. You have to do the split outside of the Parse, then parse each value separately and create your int[] from those values. Parse will only work on individual strings, not arrays of them.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakThanks, I solved it
-
I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
}But, inside the pranthesis has an error which says:
cannot convert from 'string[] to string'
an example of the XML content is:
1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872
How can I solve this problem?
A Linq based solution might be one line of code:
using System;
using System.Linq;public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int[] ints = "1,2,3,4,666".Split(',').Select(s => int.Parse(s)).ToArray();
foreach (int i in ints)
{
Console.WriteLine(i);
}
}
}"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!