0

I have a dataset, "AllEventData", and in that dataset is (for the moment) only one table called buttonData. It has 3 columns - an autoincrementing number primary key, a name (typeof(string)), and a rectangle. The third column is specifically typeof(Rectangle). I display it all with a datatablepanel.

I did the AllEventData.WriteXML method to write the file with no special parameters, and that seems to work fine - I get a file that looks like this:

<?xml version="1.0" standalone="yes"?>
<AllEventData>
  <ButtonData>
    <ID>1</ID>
    <Button_x0020_Name>sdfh</Button_x0020_Name>
    <Button_x005F_x0020_Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Location>
        <X>480</X>
        <Y>186</Y>
      </Location>
      <Size>
        <Width>95</Width>
        <Height>67</Height>
      </Size>
      <X>480</X>
      <Y>186</Y>
      <Width>95</Width>
      <Height>67</Height>
    </Button_x005F_x0020_Location>
  </ButtonData>
</AllEventData>

But when I do a AllEventData.ReadXML of the same file, it only fills the datatablepanel with the button names, not the rectangle locations.

I tried doing a write of the schema separately:

        using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
        {
            saveFileDialog1.Filter = "XML | *.xml";
            saveFileDialog1.Title = "Save File";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                AllEventData.WriteXml(saveFileDialog1.FileName);
                string xsdFileName = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.Length - 3) + "xsd";
                AllEventData.WriteXmlSchema(xsdFileName);
            }
        }

and the same basic thing in reverse to read it with a schema:

        using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
        {
            openFileDialog1.Filter = "XML | *.xml";
            openFileDialog1.Title = "Open File";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                AllEventData.Clear();
                string xsdFileName = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.Length - 3) + "xsd";
                AllEventData.ReadXmlSchema(xsdFileName);
                AllEventData.ReadXml(openFileDialog1.FileName);

            }
        }

But still not getting there.

Why isn't ReadXML working to read what WriteXML wrote?

TulsaNewbie
  • 382
  • 3
  • 15
  • 1
    what is `AllEventData`? – Daniel A. White Mar 21 '19 at 20:10
  • 1
    [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c). – Olivier Jacot-Descombes Mar 21 '19 at 20:12
  • @DanielA.White, the dataset. – TulsaNewbie Mar 21 '19 at 20:13
  • @OlivierJacot-Descombes I've read that. Not figuring out how it addresses my issue. – TulsaNewbie Mar 21 '19 at 20:15
  • 2
    XML does not have the notion of a Rectangle. As this XML is, you will have to read the values, and create a rectangle out of them `new Rectangle(x, y, width, height)`. Or maybe you should use a `XmlSerializer`. See: [How to serialize/deserialize simple classes to XML and back](https://stackoverflow.com/questions/3356976/how-to-serialize-deserialize-simple-classes-to-xml-and-back). – Olivier Jacot-Descombes Mar 21 '19 at 20:43
  • @DourHighArch rectangles are never mentioned in that article, so I would argue no. The very concept that they're not listed in XML was never mentioned anywhere I've found here or through Google until Olivier above said it. – TulsaNewbie Mar 21 '19 at 20:52
  • The link tells you how to extract the location and size values from your XML, which you need to do to construct a rectangle. – Dour High Arch Mar 21 '19 at 20:57
  • Only if you assume that C# can turn something into XML that it can't turn back into whatever it started as, which is in no way clear from that link... also, location and size are never mentioned in there (I had checked before and now) so without knowing that a rectangle isn't an actual xml thing, that link is useless. – TulsaNewbie Mar 21 '19 at 21:04
  • This article might be useful though (not directly rectangles, but points, which are one piece of a rectangle): [link](https://stackoverflow.com/questions/37606645/how-do-i-deserialize-this-xml-back-into-an-array-of-point-objects) – TulsaNewbie Mar 21 '19 at 21:18
  • 1
    XML itself knows only primitive types like string, boolean or decimal and nested tags. But XML serializers/deserializers can cope with complex types. A completely different approach would be to use WPF which uses a specialized XML called XAML to create visible UI elements. – Olivier Jacot-Descombes Mar 22 '19 at 13:23

1 Answers1

0

As Olivier notes in the comments, rectangles too complex for XML even though it can break them down and look like it's going to work just fine. There's no way it'll ever read it, even with schema, the same way it writes it. So the "trick" to reading it properly is to either:

  1. Write a code block to parse the XML as noted in the "how to parse xml" link
  2. Serialize and deserialize the rectangle as noted in the "how to serialize/deserialize" link
  3. Store the data completely differently in the table in the first place.

The latter was what I ultimately chose to do. It's really easy for me to extract the X, Y, Width, and Height from the rectangle and store those in individual columns of the table (which also let me store another piece of data I was trying to figure out how to add in), and then turn them back into rectangles later when I need them, and it makes editing the rectangles easier for the user to figure out, too.

Doing that, I'm able to simply AllEventData.WriteXML and AllEventData.ReadXML with no further code.

TulsaNewbie
  • 382
  • 3
  • 15