Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLReader from a string content

Tags:

c#

xml

xslt

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?

like image 390
matt99 Avatar asked Dec 23 '10 11:12

matt99


People also ask

How to create XmlReader?

Creates a new XmlReader instance by using the specified text reader, settings, and context information for parsing. Creates a new XmlReader instance using the specified stream, settings, and context information for parsing. Creates a new XmlReader instance using the specified stream, base URI, and settings.

How to Read XML Element value in c# using XmlReader?

Element when reader.Name == "product" => $"{reader.Name}\n", XmlNodeType. Element => $"{reader.Name}: ", XmlNodeType. Text => $"{reader. Value}\n", XmlNodeType.


2 Answers

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

like image 186
bhuang3 Avatar answered Oct 12 '22 22:10

bhuang3


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.

like image 37
fejesjoco Avatar answered Oct 12 '22 22:10

fejesjoco