XDocument Load/Parse

S-Soft 666 Reputation points
2023-02-20T05:09:00.55+00:00

Hi,

I always use XDocument.Load to load an xml from disk with no issue, but when tried to load the same xml from MemoryStream, always get exception:

MyXDocument = XDocument.Load(MyMemoryStream, LoadOptions.None)

or even:

MyXDocument = XDocument.Parse(Encoding.UTF8.GetString(MyMemoryStream.ToArray), LoadOptions.PreserveWhitespace)

Anyone knows about loading to XDocument from MemoryStream? Thanks :)

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-02-20T05:18:54.72+00:00

    If the content is valid, then try this:

    MyMemoryStream.Position = 0
    MyXDocument = XDocument.Load(MyMemoryStream, LoadOptions.None)
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.