“Root element is missing” error but I have a root element

前端 未结 3 1075
遇见更好的自我
遇见更好的自我 2021-01-07 17:21

If anyone can explain why I\'m getting a \"Root element is missing\" error when my XML document (image attached) has a root element, they win a pony which fires lazers from

3条回答
  •  遥遥无期
    2021-01-07 17:40

    I ended up creating a quick little function to reference before each new XmlReader...

    private void ResetStream()
    {
        /*
        The point of this is simply to open the stream with a StreamReader object
        and set the position of the stream to the beginning again.
        */
    
         StreamReader reader = new StreamReader(m_stream);
    
         if (reader != null)
        {
            reader.BaseStream.Position = 0;
        }
    }
    

    So when I'm working in xml I call it right before I create my reader. I always have the same stream in memory and never recreate that.

    ResetStream();
    using (XmlReader reader = XmlReader.Create(m_stream)) { reader.Read(); }
    

提交回复
热议问题