Reading an XML document from a MultipartReader only works with an intermediate StreamReader
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
When reading an XML document from a section of a `MultipartReader`, an exception stating "Root element is missing." is thrown even when there is a root element present.
### Expected Behavior
No exception is thrown and the XML document is loaded from the section.
### Steps To Reproduce
Here is a reproduction:
```csharp
using System.Xml;
using Microsoft.AspNetCore.WebUtilities;
const string xml = """
""";
var content = new StringContent(xml);
using var multipartContent = new MultipartContent("related", "asdf");
multipartContent.Add(content);
MultipartReader reader = new("asdf", multipartContent.ReadAsStream());
var section = await reader.ReadNextSectionAsync();
XmlDocument xmlDocument = new();
xmlDocument.Load(section!.Body); // throws here
```
The actual code is more complex and I can't simply pass the XML string to `XmlDocument.LoadXml()`, but this is the most minimal reproduction I could create.
However when using an intermediate `StreamReader`, the exception does not get thrown:
```diff
using System.Xml;
using Microsoft.AspNetCore.WebUtilities;
const string xml = """
""";
var content = new StringContent(xml);
using var multipartContent = new MultipartContent("related", "asdf");
multipartContent.Add(content);
MultipartReader reader = new("asdf", multipartContent.ReadAsStream());
var section = await reader.ReadNextSectionAsync();
XmlDocument xmlDocument = new();
+ using var streamReader = new StreamReader(section!.Body);
- xmlDocument.Load(section!.Body);
+xmlDocument.Load(streamReader);
```
### Exceptions (if any)
```
System.Xml.XmlException: Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)
at Program.$(String[] args) in Program.cs:line 18
at Program.(String[] args)
```
### .NET Version
8.0.100
### Anything else?
```
.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.6c33ef20
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\
.NET workloads installed:
Workload version: 8.0.100-manifests.6c33ef20
There are no installed workloads to display.
Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71
.NET SDKs installed:
6.0.417 [C:\Program Files\dotnet\sdk]
7.0.203 [C:\Program Files\dotnet\sdk]
8.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
```
Contributor guide
Assessment
This issue has not been assessed yet.