Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to read XML data f...
Finance forum
Unanswered

How to read XML data from MemoryStream

Posted on by 6,386
Hello,
 
I'm trying to read the XML content from the memory stream.
 
Below is the code I tried, but xmlStr remains empty.
_stream is System.IO.MemoryStream, which has come from "ERDocuManagementAttachingFileEventArgs _args" >> _args.getStream();
 
Which has been resolved, but since the subject is now XML, I'm creating this new Post.
 
 
My current code:
str xmlStr;
System.IO.StreamReader  sreader = new System.IO.StreamReader(_stream, System.Text.Encoding::UTF8, true);
xmlStr = sreader.ReadToEnd();
These are the Setup of Document type I use for ER generation
 
  • Anton Venter Profile Picture
    Anton Venter 18,467 Super User 2024 Season 2 on at
    How to read XML data from MemoryStream
    Hello,
     
    If the stream offset is at the end, it will return nothing as in your case. You have to set the offset to the beginning again. I usually just make a copy of the original stream like in this example below so not to interfere with the standard process.
     
    System.IO.MemoryStream streamArchive = _args.getStream();
    
    if (streamArchive.CanSeek)
    {
        streamArchive.Seek(0, System.IO.SeekOrigin::Begin); //avent: set stream position to 0 so we can read/copy it.
    }
    
    streamArchive.CopyTo(stream);
     
     

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,115 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,971 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans