IP by XML

Hello ( sorry for all the mistake i’m french :slight_smile: )

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 :wink:

What is IP ? Is that an array or a string?

@ EriSan500 (Eric) - I was about to ask the same thing…

it’s a array of string

something like this

string[] IP = new string[4];

@ Dylan - Can you supply more of the code

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 :wink:

Have you determined either from the exception message or stepping through the code with the debugger on which line the exception is being raised?

yes this one =>

byte adresse1 = Convert.ToByte(IP[0]);

It would also help to post the relevant piece of xml code

does IP have any values in it?

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.

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?

@ 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

@ taylorza - bet me to it… :slight_smile:

I cheated, I did not test, so I hope it works :slight_smile:

@ taylorza - you were right it work !!!

@ taylorza - lol - classic