开发者

Deserialize a string

开发者 https://www.devze.com 2023-03-26 20:30 出处:网络
Currently I\'m using this code to deserialize a file StreamReader str = new StreamReader(reply); System.Xml.Serialization.XmlSerializer xSerializer = new Syst开发者_如何学JAVAem.Xml.Serialization.Xml

Currently I'm using this code to deserialize a file

            StreamReader str = new StreamReader(reply);
            System.Xml.Serialization.XmlSerializer xSerializer = new Syst开发者_如何学JAVAem.Xml.Serialization.XmlSerializer(typeof(imginfo));
            imginfo res = (imginfo)xSerializer.Deserialize(str);

How should I modify the code if reply is a string and not a path to an xml file?


Basically, you use an XmlReader chained to a StringReader:

imginfo res;
using(var sr = new StringReader(xml)) // "xml" is our string containing xml
using(var xr = XmlReader.Create(sr)) {
    res = (imginfo)xSerializer.Deserialize(xr);
}
//... use "res"

or as Anders notes:

imginfo res;
using(var sr = new StringReader(xml)) // "xml" is our string containing xml
    res = (imginfo)xSerializer.Deserialize(sr);
}
//... use "res"


Use StringReader instead of StreamReader. No other change needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消