反序列化xml时路径中的非法字符?

时间:2014-07-10 06:23:18

标签: c# xml

我有一个xml,我需要反序列化为List。实际上我只是使用XSD2Code工具来生成序列化和反序列化方法。

所有工作正常但是当我传递文件名时,我得到了xmlstring但没有反序列化,因为路径中的非法字符

我的代码:

    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();
        }
    }
}

1 个答案:

答案 0 :(得分:7)

是的,现在我们已经获得了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。