Dylan
June 25, 2012, 6:51am
1
Hello ( sorry for all the mistake i’m french )
I’ve a little problem with my FEZ PANDA …
I would like to give an IP to my fez panda by a XML.
I use the XML reader that i find in the WIKI but variable return by the XML reader are string and i need to use byte …
i try this =>
byte adresse1 = Convert.ToByte(IP[0]);
byte adresse2 = Convert.ToByte(IP[1]);
byte adresse3 = Convert.ToByte(IP[2]);
byte adresse4 = Convert.ToByte(IP[3]);
byte[] ip = { adresse1, adresse2, adresse3, adresse4 };
with this in the xml reader =>
while (!xmlr.EOF)
{
xmlr.Read();
switch (xmlr.NodeType)
{
case XmlNodeType.Element:
Debug.Print("element: " + xmlr.Name);
if (xmlr.Name == "adresse1")
{
IP[0] = xmlr.Value;
}
if (xmlr.Name == "adresse2")
{
IP[1] = xmlr.Value;
}
if (xmlr.Name == "adresse3")
{
IP[2] = xmlr.Value;
}
if (xmlr.Name == "adresse4")
{
IP[3] = xmlr.Value;
}
// i don't put all the code because it's very long :-)
but it’s dosen’t work …
VS2010 say that it is a problem with => [quote]System.IndexOutOfRangeException[/quote]
thanks for help if you can understand me
What is IP ? Is that an array or a string?
Justin
June 25, 2012, 6:59am
3
@ EriSan500 (Eric) - I was about to ask the same thing…
Dylan
June 25, 2012, 7:37am
4
it’s a array of string
something like this
string[] IP = new string[4];
Justin
June 25, 2012, 7:40am
5
@ Dylan - Can you supply more of the code
Dylan
June 25, 2012, 7:44am
6
of curse
this is in the main =>
LectureXML();
byte adresse1 = Convert.ToByte(IP[0]);
byte adresse2 = Convert.ToByte(IP[1]);
byte adresse3 = Convert.ToByte(IP[2]);
byte adresse4 = Convert.ToByte(IP[3]);
byte[] ip = { adresse1, adresse2, adresse3, adresse4 };
and this is the method LectureXML() =>
static void LectureXML()
{
//***********************************************************
//***********************************************************
// test XML
//***********************************************************
MemoryStream ms = new MemoryStream();
byte[] byteArray = ms.ToArray();
char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(@ "\SD\CONF\fichierConfiguration.xml",
FileMode.Open);
///////////read xml
MemoryStream rms = new MemoryStream(byteArray);
XmlReaderSettings ss = new XmlReaderSettings();
ss.IgnoreWhitespace = true;
ss.IgnoreComments = false;
//XmlException.XmlExceptionErrorCode.
XmlReader xmlr = XmlReader.Create(FileHandle);
while (!xmlr.EOF)
{
xmlr.Read();
switch (xmlr.NodeType)
{
case XmlNodeType.Element:
Debug.Print("element: " + xmlr.Name);
if (xmlr.Name == "adresse1")
{
IP[0] = xmlr.Value;
}
if (xmlr.Name == "adresse2")
{
IP[1] = xmlr.Value;
}
if (xmlr.Name == "adresse3")
{
IP[2] = xmlr.Value;
}
if (xmlr.Name == "adresse4")
{
IP[3] = xmlr.Value;
}
if (xmlr.Name == "check_AttenteBeton")
{
check_AttenteBeton = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_Palettisation")
{
check_Palettisation = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_fabricationPlanche")
{
check_fabricationPlanche = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_AttentePlanche")
{
check_AttentePlanche = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_EvacPlanche")
{
check_EvacPlanche = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_BPfinPoste")
{
check_BPfinPoste = ToInt32(xmlr.Value);
}
if (xmlr.Name == "check_groupeHydraulique")
{
check_groupeHydraulique = ToInt32(xmlr.Value);
}
break;
case XmlNodeType.Text:
Debug.Print("text: " + xmlr.Value);
break;
default:
Debug.Print(xmlr.NodeType.ToString());
break;
}
}
}
if you want more ask but there is more than 1000 line of code
Have you determined either from the exception message or stepping through the code with the debugger on which line the exception is being raised?
Dylan
June 25, 2012, 7:50am
8
yes this one =>
byte adresse1 = Convert.ToByte(IP[0]);
It would also help to post the relevant piece of xml code
Justin
June 25, 2012, 7:52am
10
does IP have any values in it?
Dylan
June 25, 2012, 7:54am
11
here is the XML
<?xml version="1.0" encoding="utf-8" ?>
<XMLConfigurationFEZ>
<adresse1>192</adresse1>
<adresse2>168</adresse2>
<adresse3>1</adresse3>
<adresse4>1</adresse4>
<check_AttenteBeton>1</check_AttenteBeton>
<check_Palettisation>0</check_Palettisation>
<check_fabricationPlanche>0</check_fabricationPlanche>
<check_AttentePlanche>0</check_AttentePlanche>
<check_EvacPlanche>0</check_EvacPlanche>
<check_BPfinPoste>0</check_BPfinPoste>
<check_groupeHydraulique>1</check_groupeHydraulique>
</XMLConfigurationFEZ>
and no when i look with debug there is nothing in IP …
This would indicate the the IP array has been allocated with 0 elements. Can you put a break point on that line and inspect the IP.Length property and see what it returns to you.
Justin
June 25, 2012, 8:00am
13
The addresses are sub nodes of - is that the issue.
I did a quick test, and I see that Convert.ToByte() throws an IndexOutOfRangeException if you pass it an empty string, so it might be an issue with the parsing of the XML, have you inspected the values in the IP array to check that they have been read correctly?
Dylan
June 25, 2012, 8:07am
15
@ justin : i don’t understand why it coud be the problem ?
@ taylorza when i inspect IP it’s empty
and yes i have the same problem that you have when you make your test
I checked your XML parsing code, the problem is that you are trying to access the Value property which is typically used for XML attributes, try the following
while (xmlr.Read())
{
switch (xmlr.NodeType)
{
case XmlNodeType.Element:
Debug.Print("element: " + xmlr.Name);
if (xmlr.Name == "adresse1")
{
IP[0] = xmlr.ReadElementString();
}
if (xmlr.Name == "adresse2")
{
IP[1] = xmlr.ReadElementString();
}
if (xmlr.Name == "adresse3")
{
IP[2] = xmlr.ReadElementString();
}
if (xmlr.Name == "adresse4")
{
IP[3] = xmlr.ReadElementString();
}
break;
}
}
1 Like
Justin
June 25, 2012, 8:13am
17
@ taylorza - bet me to it…
I cheated, I did not test, so I hope it works
Dylan
June 25, 2012, 8:16am
19
@ taylorza - you were right it work !!!
Justin
June 25, 2012, 8:20am
20
@ taylorza - lol - classic