locked
Token StartElement in state EndRootElement would result in an invalid XML document RRS feed

  • Question

  • Hi,

    I'm writing a code to get joints information of a body using kinect, and save these information in an XML file then send this data over TCP/IP.

    well My problem is, when writing elements in xml I get "Token StartElement in state EndRootElement would result in an invalid XML document"

    here is my code:

            
     private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
            {
                bool dataReceived = false;
                double timeStamp = Stopwatch.GetTimestamp();
                writer.WriteStartElement("KinectSkeletalAnimation");
                {
                    //writer.WriteWhitespace("\n");
                    writer.WriteStartElement("Frame"); // HERE I GET THE EXCEPTION in the second loop
                    {
                        writer.WriteAttributeString("FrameNumber", nFrame.ToString());
                        writer.WriteAttributeString("TimeStamp", timeStamp.ToString());
                        //writer.WriteWhitespace("\n");
                        using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
                        {
                            if (bodyFrame != null)
                            {
                                if (this.bodies == null)
                                {
                                    this.bodies = new Body[bodyFrame.BodyCount];
                                }
                                // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                                // As long as those body objects are not disposed and not set to null in the array,
                                // those body objects will be re-used.
                                bodyFrame.GetAndRefreshBodyData(this.bodies);
                                dataReceived = true;
                            }
                        }
    
                        if (dataReceived)
                        {
                            using (DrawingContext dc = this.drawingGroup.Open())
                            {
                                // Draw a transparent background to set the render size
                                dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
    
                                int penIndex = 0;
                                foreach (Body body in this.bodies)
                                {
                                    Pen drawPen = this.bodyColors[penIndex++];
    
                                    if (body.IsTracked)
                                    {
                                        this.DrawClippedEdges(body, dc);
    
                                        IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
    
                                        // convert the joint points to depth (display) space
                                        Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
    
                                        foreach (JointType jointType in joints.Keys)
                                        {
                                            // sometimes the depth(Z) of an inferred joint may show as negative
                                            // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                                            CameraSpacePoint position = joints[jointType].Position;
                                            if (position.Z < 0)
                                            {
                                                position.Z = InferredZPositionClamp;
                                            }
                                            DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                                            jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
                                        }
                                        writer.WriteStartElement("Skeleton");
                                        {
                                            writer.WriteAttributeString("ID", "1");
                                            //writer.WriteWhitespace("\n");
                                            this.DrawBody(joints, jointPoints, dc, drawPen);
                                        }
                                        writer.WriteEndElement(); // Skeleton
                                        this.DrawHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
                                        this.DrawHand(body.HandRightState, jointPoints[JointType.HandRight], dc);
                                    }
                                }
                                // prevent drawing outside of our render area
                                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
                            }
                        }
                    }
                    nFrame++;
                    writer.WriteEndElement(); // Frame
                    //writer.WriteWhitespace("\n");
                }
                writer.WriteEndElement(); // KinectSkeletalAnimation
                //writer.WriteWhitespace("\n");
            }

    I get an exception in the second loop.

    can anyone help?

    thank you in advance,

    Sohaib





    • Edited by sohaib-l Wednesday, August 20, 2014 9:53 AM adding code block
    Wednesday, August 20, 2014 9:36 AM

Answers

  • This error occurs because you are trying to insert second root element and valid XML file can have only one. To solve this problem you can insert first start element after initialization of writer object and close it just before saving XML file.
    • Marked as answer by sohaib-l Wednesday, August 20, 2014 10:12 AM
    Wednesday, August 20, 2014 9:59 AM

All replies

  • This error occurs because you are trying to insert second root element and valid XML file can have only one. To solve this problem you can insert first start element after initialization of writer object and close it just before saving XML file.
    • Marked as answer by sohaib-l Wednesday, August 20, 2014 10:12 AM
    Wednesday, August 20, 2014 9:59 AM
  • Perfect! thank you Mr. Zator, it solved my problem.
    Wednesday, August 20, 2014 10:12 AM