IndexOf - " Index and length must refer to a location within the string."

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey all...
I am trying to make a parser that will fetch my xml-like data.

<div style="color:black; background-color:white
<pre> WebClient^ client = <span style="color:blue gcnew WebClient;

String^ pageData = <span style="color:#a31515 "http://eblox.net/game/fetchxml.php?serverId="+<span style="color:blue this->serverId->Text+<span style="color:#a31515 "?refresh=f5";
String^ pageHtml = client->DownloadString( pageData );

<span style="color:green // PARSE //
<span style="color:blue while (pageHtml->IndexOf(<span style="color:#a31515 ") != -1 && pageHtml->IndexOf(<span style="color:#a31515 ") != 0) {
String^ brick_position = <span style="color:#a31515 "";
String^ brick_file = <span style="color:#a31515 "";

<span style="color:green // Tags: <brick> <file></file> <position></position> </brick> //

<span style="color:blue int brickTagBegin = pageHtml->IndexOf(<span style="color:#a31515 "<brick>"); <span style="color:green // len: 7
<span style="color:blue int brickTagEnd = pageHtml->IndexOf(<span style="color:#a31515 "</brick>"); <span style="color:green // len: 8
String^ testOne = <span style="color:#a31515 "This is NOT a TEST"; <span style="color:green // IndexOf NOT = 8
String^ brick_settings = pageHtml->Substring((brickTagBegin+7),(brickTagEnd-7));
String^ brick_settings2 = pageHtml->Substring((brickTagBegin+7),(brickTagEnd-7));

<span style="color:blue int fileDataBegin = brick_settings->IndexOf(<span style="color:#a31515 "<file>")+6;
<span style="color:blue int fileDataEnd = brick_settings->IndexOf(<span style="color:#a31515 "</file>")-6;
String^ fileText = brick_settings->Substring(fileDataBegin, fileDataEnd);

brick_settings2 = brick_settings->Substring(fileDataEnd+6);
<span style="color:blue int positionDataBegin = 17;
<span style="color:blue int positionDataEnd = brick_settings2->Length-28;
String^ positionText = brick_settings2->Substring(positionDataBegin, positionDataEnd);


<span style="color:blue int posComma = positionText->IndexOf(<span style="color:#a31515 ",");

MessageBox::Show(<span style="color:#a31515 """+positionText+<span style="color:#a31515 "" - posComma = "+posComma.ToString());

Int32 positionX;
Int32 positionY;

positionX = System::Convert::ToInt32(positionText->Substring(0,posComma));

positionY = System::Convert::ToInt32(positionText->Substring(posComma+1,positionText->Length-1));

<span style="color:green //Object^ brickItem = gcnew System::Windows::Forms::PictureBox();
<span style="color:green // 1x1 stud = 12,13 (X,Y)
<span style="color:green // 2x2 studs = 28,28 (X,Y)

<span style="color:green //GameBricks->Insert( GameBricks->Count, brickItem );

pageHtml = pageHtml->Substring(brickTagEnd+8);

}
[/code]



The server is currently set to reply the following:

<div style="color:black; background-color:white
<pre><span style="color:blue <<span style="color:#a31515 brick<span style="color:blue ><span style="color:blue <<span style="color:#a31515 file<span style="color:blue >textures/parts/2x2x2/28x28_2x2x2_blue.png<span style="color:blue </<span style="color:#a31515 file<span style="color:blue ><span style="color:blue <<span style="color:#a31515 position<span style="color:blue >60,00<span style="color:blue </<span style="color:#a31515 position<span style="color:blue ><span style="color:blue </<span style="color:#a31515 brick<span style="color:blue >
[/code]


Unfortunately, the following line is returning the error in the title of this thread.

<div style="color:black; background-color:white
<pre>positionY = System::Convert::ToInt32(positionText->Substring(posComma+1,positionText->Length-1));
[/code]


posComma s position IS the location of the "," between the position tags.
I have used a MessageBox to show me what posComma is, and what positionText->Length is- it all fits in the string.
What is happening?
<
Newbie at Visual C++ - Trying to accomplish a dream, but need a little help.<br/>

View the full article
 
Back
Top