简体   繁体   中英

De-serialize XML String

Hello all I have the following XML string generated from file. I want to deserialize it.

<CustomerName>TEST</CustomerName>
<PONumber></PONumber>
<ProcessedBy>Jerry Cooke</ProcessedBy>
<ProcessDate>03-05-2004 14:00:49</ProcessDate>
<TagNumber></TagNumber>
<SerialNumber>134</SerialNumber>

I am using the following code.

string Data =  upperxmlstring
 XmlTextReader reader = new XmlTextReader(Data);
                obj = (T)serializer.Deserialize(reader);
                reader.Close();

I am getting following exception " Illegal characters in path. " This error comes at XmlTextReader reader = new XmlTextReader(Data); Please help me in solving it.

new XmlTextReader(string) expects a filename, not the content. To read the content from a string you'll have to instantiate a TextReader for that string. Use StringReader for that.

Better still, don't use XmlTextReader , since it's been deprecated. Use XmlReader.Create instead:

string Data = upperxmlstring;
XmlReader reader = XmlReader.Create(new StringReader(Data));
obj = (T)serializer.Deserialize(reader);
reader.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM