反序列化xml时路径中的非法字符
本文关键字:非法 字符 路径 xml 反序列化 | 更新日期: 2023-09-27 18:12:11
我有一个xml,我需要反序列化为列表。实际上,我只是使用XSD2Code工具来生成序列化和反序列化方法。
都工作得很好,但当我传递文件名时,我得到了xmlstring,但没有反序列化,因为路径
中的非法字符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();
}
}
}
这里我得到了xmlString和字符串我得到的是…
"<?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>"
我的原始xml文件是
<?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>
请帮我找出错误
更新:新增反序列化方法
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();
}
}
}
好了,现在我们有了Deserialize
方法,问题很明显:
stream = new StreamReader(xml);
这将xml
视为文件名,而不是XML。或者你只需要输入:
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);
}
}
…或使其接受XML,并使用StringReader
:
public T Deserialize(string xml)
{
var serializer = new XmlSerializer(typeof(T));
return (T) serializer.Deserialize(new StringReader(xml));
}
我个人更喜欢前一个版本-它允许XML解析代码自动检测编码,而目前当您将文件作为字符串读取时,假设是UTF-8。