Search code examples
c#xmlcheckboxxmlreader

CreateNewCheckBox with Text from XML


I'm kinda stuck with reading the value of a xml-file. The XML looks like:

<?xml version="1.0" encoding="utf-16"?>
<spfFiles>
  <file>200_006 xxxxxxx</file>
  <file>200_010 xxxxxxx</file>
  <file>200_022 xxxxxxx</file>
  <file>200_023 xxxxxxx</file>
  <file>200_024 xxxxxxx</file>
  <file>200_031 xxxxxxx</file>
  <file>200_041 xxxxxxx</file>
</spfFiles>

What I'm trying to do is that I want to creat a new Checkbox for each file.

 XmlTextReader xReader = new XmlTextReader("spfFiles_simW.xml");
            while (xReader.Read())
            {
                switch (xReader.NodeType)
                {
                    case XmlNodeType.Text: //Display the text in each element.
                        pnlSPFLIST.Controls.Add(CreateNewCheckBox(xReader.Value));
                        break;
                }
            }

The creation of each new element works fine. But I've got trouble with the file names. Each Checkbox only gets the part before the space as a name. For example "200_006". My xmlReader somehow seems to cut the rest of.

Edit: So here is my CreateNewCheck

private CheckBox CreateNewCheckBox(string sName)
        {
            label1.Text = sName;
            int iExistingCheckBoxX = 0;
            int iExistingCheckBoxY = 0;

            int iIncrementX = 100;
            int iIncrementY = 20;

            CheckBox cbNew = new CheckBox();

            cbNew.Width = iIncrementX;

            if (pnlSPFLIST.Controls.Count == 0)
            {
                cbNew.Location = new Point(pnlSPFLIST.Location.X, pnlSPFLIST.Location.Y-25);
            }
            else
            {
                // Existing checkboxes, so get the Location of the last one.
                iExistingCheckBoxX = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.X;
                iExistingCheckBoxY = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.Y;

                iExistingCheckBoxX = pnlSPFLIST.Location.X;
                iExistingCheckBoxY = iExistingCheckBoxY + iIncrementY + 10;

                cbNew.Location = new Point(iExistingCheckBoxX, iExistingCheckBoxY);

            }

            // Set the Text property according to the input.
            cbNew.Text = sName;

            return cbNew;
        }

Does anyone know where I've gone wrong?


Solution

  • So here is the right solution. I Just had to change my CreateNewCheckBox-Method:

    private CheckBox CreateNewCheckBox(string sName)
            {
               ...
    
                int iIncrementX = 300;
                int iIncrementY = 20;
    
               ...
    
            }
    

    Just increase the X-Value and everything works fine.