- Home /
How to Deserialize an XML textasset?
So I have this code:
var serializer = new XmlSerializer(typeof(MyClass));
using(var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as MyClass;
}
Basically, it reads an xml file from "path" and returns it as a MyClass. But what if instead of having the file's path, I have a public TextAsset variable referencing the actual file? How could I deserialize the "textasset" xml file into MyClass?
Answer by Bunny83 · Nov 18, 2013 at 01:28 PM
Well, all you need is to convert the string into a stream. The string can be taken from TextAsset.text
edit
btw the Deserialize method has 6 overloads and one takes a TextReader. TextReader is an abstract base class. You cah use a StringReader (which ia a TextReader) like this:
var serializer = new XmlSerializer(typeof(MyClass));
using(var reader = new System.IO.StringReader(yourTextAsset.text))
{
return serializer.Deserialize(reader) as MyClass;
}
Answer by JohnnySunshine · Nov 18, 2013 at 01:38 PM
Use System.IO.StringReader along with TextAsset.text to parse the contents.
TextAsset PublicAsset;
MyClass Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
using (System.IO.StringReader reader = new System.IO.StringReader(PublicAsset.text))
{
return serializer.Deserialize(reader) as MyClass;
}
}
Your answer
Follow this Question
Related Questions
Dtd validation with TextAsset xml gives exception 0 Answers
how can i deserialize xml to vector4 List?? 0 Answers
Serialize object to XML file specified not by path, but by TextAsstet 0 Answers
Need Help Serializing/Deserializing XML 0 Answers
Does XML Serialization Support "Class Object" Arrays? 1 Answer