I have one xml and i need to deserialize as List. Actually i just used XSD2Code tool to generate the serialize and deserialize methods.
All are working fine but when i pass the filename, i got the xmlstring but not deserialized because of illegal characters in the path
My Code :
public T LoadFromFile(string fileName)
{
System.IO.FileStream file = null;
System.IO.StreamReader sr = null;
try
{
file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
sr = new System.IO.StreamReader(file);
string xmlString = sr.ReadToEnd();
sr.Close();
file.Close();
return Deserialize(xmlString);
}
finally
{
if ((file != null))
{
file.Dispose();
}
if ((sr != null))
{
sr.Dispose();
}
}
}
Here i got the xmlString and the string i got is ...
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ZTIMES_SQL_TO_SAP_CELL.Response xmlns=\"urn:sap-com:document:sap:rfc:functions\">\r\n <IT_RETURN xmlns=\"\">\r\n <item>\r\n <INAME>CELLCOMPLETION</INAME>\r\n <UNQ_ID>1234</UNQ_ID>\r\n <STATUS>F</STATUS>\r\n </item>\r\n <item>\r\n <INAME>CELLCOMPLETION</INAME>\r\n <UNQ_ID>1235</UNQ_ID>\r\n <STATUS>S</STATUS>\r\n </item>\r\n <item>\r\n <INAME>CELLCOMPLETION</INAME>\r\n <UNQ_ID>1236</UNQ_ID>\r\n <STATUS>F</STATUS>\r\n </item>\r\n </IT_RETURN>\r\n <IT_CELL_COMP xmlns=\"\">\r\n <item>\r\n <UNQ_ID>1234</UNQ_ID>\r\n <LOC_CODE>LOC1</LOC_CODE>\r\n <TRSLNO>TRSLNO1</TRSLNO>\r\n <MODEL>MODEL1</MODEL>\r\n <CELL>CELL1</CELL>\r\n <CDATE>1900-01-01</CDATE>\r\n <COMP_STATUS>COMP_STAT1</COMP_STATUS>\r\n <NEQUNR>NEQUNR1</NEQUNR>\r\n <SHIFT>1</SHIFT>\r\n <ITERATION>1</ITERATION>\r\n <CREATED_BY>CREATED_BY1</CREATED_BY>\r\n <CREATED_DT>1900-01-01</CREATED_DT>\r\n <CREATED_TM>01:01:01</CREATED_TM>\r\n </item>\r\n <item>\r\n <UNQ_ID>1235</UNQ_ID>\r\n <LOC_CODE>LOC2</LOC_CODE>\r\n <TRSLNO>TRSLNO2</TRSLNO>\r\n <MODEL>MODEL2</MODEL>\r\n <CELL>CELL2</CELL>\r\n <CDATE>1900-01-01</CDATE>\r\n <COMP_STATUS>COMP_STAT2</COMP_STATUS>\r\n <NEQUNR>NEQUNR2</NEQUNR>\r\n <SHIFT>-79228162514264337593543950335</SHIFT>\r\n <ITERATION>-79228162514264337593543950335</ITERATION>\r\n <CREATED_BY>CREATED_BY2</CREATED_BY>\r\n <CREATED_DT>1900-01-01</CREATED_DT>\r\n <CREATED_TM>01:01:01</CREATED_TM>\r\n </item>\r\n <item>\r\n <UNQ_ID>1236</UNQ_ID>\r\n <LOC_CODE>LOC3</LOC_CODE>\r\n <TRSLNO>TRSLNO3</TRSLNO>\r\n <MODEL>MODEL3</MODEL>\r\n <CELL>CELL3</CELL>\r\n <CDATE>0001-01-01</CDATE>\r\n <COMP_STATUS>COMP_STAT3</COMP_STATUS>\r\n <NEQUNR>NEQUNR3</NEQUNR>\r\n <SHIFT>79228162514264337593543950335</SHIFT>\r\n <ITERATION>79228162514264337593543950335</ITERATION>\r\n <CREATED_BY>CREATED_BY3</CREATED_BY>\r\n <CREATED_DT>1900-01-01</CREATED_DT>\r\n <CREATED_TM>01:01:01</CREATED_TM>\r\n </item>\r\n </IT_CELL_COMP>\r\n</ZTIMES_SQL_TO_SAP_CELL.Response>"
My Original xml file is
<?xml version="1.0" encoding="utf-8"?>
<ZTIMES_SQL_TO_SAP_CELL.Response xmlns="urn:sap-com:document:sap:rfc:functions">
<IT_RETURN xmlns="">
<item>
<INAME>CELLCOMPLETION</INAME>
<UNQ_ID>1234</UNQ_ID>
<STATUS>F</STATUS>
</item>
<item>
<INAME>CELLCOMPLETION</INAME>
<UNQ_ID>1235</UNQ_ID>
<STATUS>S</STATUS>
</item>
<item>
<INAME>CELLCOMPLETION</INAME>
<UNQ_ID>1236</UNQ_ID>
<STATUS>F</STATUS>
</item>
</IT_RETURN>
<IT_CELL_COMP xmlns="">
<item>
<UNQ_ID>1234</UNQ_ID>
<LOC_CODE>LOC1</LOC_CODE>
<TRSLNO>TRSLNO1</TRSLNO>
<MODEL>MODEL1</MODEL>
<CELL>CELL1</CELL>
<CDATE>1900-01-01</CDATE>
<COMP_STATUS>COMP_STAT1</COMP_STATUS>
<NEQUNR>NEQUNR1</NEQUNR>
<SHIFT>1</SHIFT>
<ITERATION>1</ITERATION>
<CREATED_BY>CREATED_BY1</CREATED_BY>
<CREATED_DT>1900-01-01</CREATED_DT>
<CREATED_TM>01:01:01</CREATED_TM>
</item>
<item>
<UNQ_ID>1235</UNQ_ID>
<LOC_CODE>LOC2</LOC_CODE>
<TRSLNO>TRSLNO2</TRSLNO>
<MODEL>MODEL2</MODEL>
<CELL>CELL2</CELL>
<CDATE>1900-01-01</CDATE>
<COMP_STATUS>COMP_STAT2</COMP_STATUS>
<NEQUNR>NEQUNR2</NEQUNR>
<SHIFT>-79228162514264337593543950335</SHIFT>
<ITERATION>-79228162514264337593543950335</ITERATION>
<CREATED_BY>CREATED_BY2</CREATED_BY>
<CREATED_DT>1900-01-01</CREATED_DT>
<CREATED_TM>01:01:01</CREATED_TM>
</item>
<item>
<UNQ_ID>1236</UNQ_ID>
<LOC_CODE>LOC3</LOC_CODE>
<TRSLNO>TRSLNO3</TRSLNO>
<MODEL>MODEL3</MODEL>
<CELL>CELL3</CELL>
<CDATE>0001-01-01</CDATE>
<COMP_STATUS>COMP_STAT3</COMP_STATUS>
<NEQUNR>NEQUNR3</NEQUNR>
<SHIFT>79228162514264337593543950335</SHIFT>
<ITERATION>79228162514264337593543950335</ITERATION>
<CREATED_BY>CREATED_BY3</CREATED_BY>
<CREATED_DT>1900-01-01</CREATED_DT>
<CREATED_TM>01:01:01</CREATED_TM>
</item>
</IT_CELL_COMP>
</ZTIMES_SQL_TO_SAP_CELL.Response>
please help me to identy the error
Update: Added Deserialize Method
public T Deserialize(string xml)
{
// System.IO.StringReader stringReader = null;
StreamReader stream = null;
try
{
Serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
stream = new StreamReader(xml);
// stringReader = new System.IO.StringReader(xml);
return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stream))));
}
finally
{
if ((stream != null))
{
stream.Dispose();
}
}
}
Right, now we've got your Deserialize
method, the problem is obvious:
stream = new StreamReader(xml);
That's treating xml
as a filename, not as XML. Either you should just have:
public T Deserialize(string filename)
{
// There's no need to make the serializer a property or instance variable...
var serializer = new XmlSerializer(typeof(T));
using (var stream = File.OpenRead(filename))
{
return (T) serializer.Deserialize(stream);
}
}
... or make it take XML, and use a StringReader
:
public T Deserialize(string xml)
{
var serializer = new XmlSerializer(typeof(T));
return (T) serializer.Deserialize(new StringReader(xml));
}
Personally I prefer the former version - it allows the XML parsing code to automatically detect encodings, whereas currently you're assuming UTF-8 when you read the file as a string.
See more on this question at Stackoverflow