Jump to content

Does anyone know what's going on with this code.

- - - - -

  • Please log in to reply
5 replies to this topic

#1
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
I know I'm going to kick myself when the answer comes but, what's going on here.

I use this code to save:
XmlTextWriter writer = new XmlTextWriter(fileName, null);

            writer.WriteStartDocument();

            writer.WriteStartElement("CollisionGeom");

            foreach (Rectangle rect in rects)

            {

                writer.WriteStartElement("Rect");

                writer.WriteAttributeString("x", rect.X.ToString());

                writer.WriteAttributeString("y", rect.Y.ToString());

                writer.WriteAttributeString("width", rect.Width.ToString());

                writer.WriteAttributeString("height", rect.Height.ToString());

                writer.WriteEndElement();

            }

            writer.WriteEndElement();

            writer.WriteEndDocument();
and this code to load:
 XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(fileName);

            XmlNodeList xmlRects = xmlDoc.SelectNodes("CollisionGeom");

            foreach (XmlNode xmlRect in xmlRects)

            {

                int x = int.Parse(xmlRect.Attributes["x"].Value);

                int y = int.Parse(xmlRect.Attributes["y"].Value);

                int width = int.Parse(xmlRect.Attributes["width"].Value);

                int height = int.Parse(xmlRect.Attributes["height"].Value);

                Rectangle rect = new Rectangle(x, y, width, height);

                rects.Add(rect);

                Graphics formGraphics = this.CreateGraphics();

                SolidBrush myBrush = new SolidBrush(Color.Blue);

                formGraphics.FillRectangle(myBrush, rect);

            }
But the "xmlDoc.Load(fileName);" throws an exception that says the root element is missing. Obviously the problem is in the Save method somewhere, but no matter how many times I look, I must just miss it:crying:. li'l help please.

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Testing your code doesn't give me the error you are having (it gives one relating to your parsing which you are doing wrong). I can't help with the error you are having, but I can with the one you will have :)

Your line that performs the node selection SelectNodes("CollisionGeom") selects the "CollisionGeom" node, which does not have attributes "x", "y", etc. You'll need to iterate through each returned node and do another SelectNodes on "Rect" which does have those attributes.

#3
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
Thanks. :) that should be "CollisionGeom\Rect". If the file was written by this program, it should only have one CollisionGeom element.

#4
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
I found something when I went back to test it. When I hovered over the new file in the open dialogbox, it told me it's size was 0 bytes. I opened it up in notepad and sure enough, it was empty (should have tried that first come to think of it). I still don't know what's going on but I hope that helps.

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Do you have a writer.Close() somewhere in your code? Your save code doesn't close the writer object properly (which means nothing will be written).

#6
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
Thanks. That fixed it.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users