Skip to content

XmlReader.ReadContentAs{Async} different behavior #52646

@kronic

Description

@kronic

There is a difference in reading content depending on the settings XmlReaderSettings

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

const string xml = "<test a=\"sdf\"/>";
//1
try
{
	using var xmlReader = XmlReader.Create(new StringReader(xml));
	xmlReader.MoveToContent();
	xmlReader.MoveToAttribute(0);

	if (xmlReader.ReadContentAs(typeof(XmlAtomicValue), null) is XmlAtomicValue xmlAtomic)
	{
//{"Content cannot be converted to the type System.Xml.Schema.XmlAtomicValue. Line 1, position 7."}
		Console.WriteLine(xmlAtomic.Value);
	}
}
catch (Exception ex)
{
	Console.WriteLine(ex);
}
//2
try
{
	using var xmlReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings {Async = true});
	await xmlReader.MoveToContentAsync();
	xmlReader.MoveToAttribute(0);

	if (await xmlReader.ReadContentAsAsync(typeof(XmlAtomicValue), null) is XmlAtomicValue xmlAtomic)
	{
//ok
		Console.WriteLine(xmlAtomic.Value);
	}
}
catch (Exception ex)
{
	Console.WriteLine(ex);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions