.NET 6 - reading Xml with schema as string

Viewed 20

I'm trying to load an xml file which schema comes as a string:

using (MemoryStream memStream = GenerateStreamFromString(XsdSchemas.the_schema_string))
{
   // tried this one too => using TextReader txtReader = new StreamReader(memStream);
   using XmlTextReader xmlReader = new(memStream);
   settings.Schemas.Add(THE_NAMESPACE_S, xmlReader);
}

private static MemoryStream GenerateStreamFromString(string value)
{
   if (string.IsNullOrWhiteSpace(value))
      throw new ArgumentNullException("value");
   MemoryStream memStream = new(Encoding.UTF8.GetBytes(value));
   memStream.Position = 0; // just in case
   return memStream;
}

But I can't get it to work. The debug shows xmlReader as "None". I have no idea what that means.

1 Answers

You're making this far too complicated.

Use new XmlTextReader(new StringReader(theString)).

The MemoryStream is completely unnecessary. Having said that, I don't know why it's failing.

Related