Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# - Using Memory Streams with XML [modified]

C# - Using Memory Streams with XML [modified]

Scheduled Pinned Locked Moved C#
helpcsharpxmlperformancetutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Ostrich22
    wrote on last edited by
    #1

    I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

    modified on Tuesday, October 20, 2009 9:58 AM

    K C OriginalGriffO M L 5 Replies Last reply
    0
    • O Ostrich22

      I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

      modified on Tuesday, October 20, 2009 9:58 AM

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      The current position pointer of the memory stream is left at the end when you do the ds.WriteXml. You have to do something (apologies, no access to IDE right now) like ms.CurrentPosition = 0; before the the ReadXml to move it back to the start.

      CCC solved so far: 2 (including a Hard One!)

      1 Reply Last reply
      0
      • O Ostrich22

        I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

        modified on Tuesday, October 20, 2009 9:58 AM

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #3

        In your example you try to read after the written data (MemoryStream position != 0). You have to reset the read position of the memory stream.

        MemoryStream ms = new MemoryStream();
        ds.WriteXml(ms);
        ms.Seek(0, SeekOrigin.Begin);
        ds.ReadXml(ms);

        O 1 Reply Last reply
        0
        • C Covean

          In your example you try to read after the written data (MemoryStream position != 0). You have to reset the read position of the memory stream.

          MemoryStream ms = new MemoryStream();
          ds.WriteXml(ms);
          ms.Seek(0, SeekOrigin.Begin);
          ds.ReadXml(ms);

          O Offline
          O Offline
          Ostrich22
          wrote on last edited by
          #4

          Aah yes, silly me, I forgot about the pointer. Thank you!

          1 Reply Last reply
          0
          • O Ostrich22

            I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

            modified on Tuesday, October 20, 2009 9:58 AM

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Keefb is coirrect, you have to rewind the stream, using:

            if (ms.CanSeek)
            {
            ms.Seeek(0,SeekOrigin.Begin);
            }

            But be aware it may not work! You may well need ms.FlushFinalBlock() to ensure that last bit of data is written.

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • O Ostrich22

              I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

              modified on Tuesday, October 20, 2009 9:58 AM

              M Offline
              M Offline
              Mirko1980
              wrote on last edited by
              #6

              If you look at the value of ms.Position after you have wrote in it, you'll see that has the same value of ms.Length. That is because you have just written into the stream and the stream cursor is located at the end. Due to that, if you try to read the stream content, the reader doesn't found anything to read, giving the "Root Element is missing" exception. Call ms.Seek(0, SeekOrigin.Begin) (or set ms.Position = 0) to move the cursor back to the beginning of the stream after writing and before reading.

              1 Reply Last reply
              0
              • O Ostrich22

                I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code. DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs. Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?

                modified on Tuesday, October 20, 2009 9:58 AM

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                if all else fails, maybe try and set ms.Position=0; :laugh:

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups