显示" xml文档中存在错误(0,0)"而XMlFile正在阅读

时间:2016-09-07 09:40:28

标签: c# xml c#-4.0

读取转换为String的XML文件。

我的代码如下

StringBuilder sb = new StringBuilder();
string xmlVal=string.Empty;

using (var reader = SqlHelper.ExecuteXmlReader(Conn, CommandType.StoredProcedure, spName, ListParam.ToArray()))
{
    while (reader.Read())
    {
        sb.AppendLine(reader.ReadOuterXml());
        xmlVal = sb.ToString(); // You can get the xml as string here.
    }
}

MemoryStream mStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mStream, System.Text.Encoding.Unicode);
XmlDocument document = new XmlDocument();                   

document.LoadXml(xmlVal);
writer.Formatting = Formatting.Indented;                       
document.WriteContentTo(writer);
writer.Flush();
mStream.Flush();                     
mStream.Position = 0;                      
StreamReader sReader = new StreamReader(mStream);

输入此代码显示错误there is an error in xml document(0,0),但该字符串包含XMLCollection(xmlVal

XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<CollectionlDetail>));
using (StreamReader rd = new StreamReader(mStream))
{
    CollectionlDetailCol= xs.Deserialize(rd) as ObservableCollection<CollectionlDetail>;
}

0 个答案:

没有答案