XMLReader from a string content


C# Problem Overview


I'm trying to generate XML from another XML using a XslTransform. I get both files (source XML and XSL transformation file) as string content, so I'm trying to pass the XSL file to XslTransform.Load() method as XmlReader. Now the XmlReader has to be created form a source string containing XSL file, so i try doing it like this:

MemoryStream memStream = new MemoryStream();
byte[] data = Encoding.Default.GetBytes(transformation.XsltContent);
memStream.Write(data, 0, data.Length);
memStream.Position = 0;
XmlReader reader = XmlReader.Create(memStream);

and also tried using a StringReader:

XmlReader reader = XmlReader.Create(new StringReader(transformation.XsltContent));

Unfortunately, bot methods don't seems to work, the input seems to be ok, I even tried creating some basic one-element XML to pass, won't work either - reader contains {None}.

Could someone point out what seems to be the problem here?

C# Solutions

Solution 1 - C#

XmlReader xmlReader = XmlReader.Create(new StringReader(YourStringValue));

Solution 2 - C#

The StringReader -> XmlReader approach is fine, you should stick to it. The reader reports none because it hasn't been read yet. Try calling Read() on it to see what happens then. The transformation will also call read on it.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmatt99View Question on Stackoverflow
Solution 1 - C#bhuang3View Answer on Stackoverflow
Solution 2 - C#fejesjocoView Answer on Stackoverflow