C# 存在一个未关闭的文本字符串。第1行,位置12284
我有一个从web服务生成的类:C# 存在一个未关闭的文本字符串。第1行,位置12284,c#,.net,xml,C#,.net,Xml,我有一个从web服务生成的类: [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class RequestMessage
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="urn:ihe:iti:xds-b:2007", Order=0)]
public Helper.RequestType Request;
public RequestMessage() {
}
}
我正在尝试通过以下方式创建XmlDocument:
XmlDocument xReq = new XmlDocument();
DataContractSerializer serializer = new DataContractSerializer(typeof(RequestMessage));
using (MemoryStream memStm = new MemoryStream())
{
using (XmlWriter xw = XmlWriter.Create(memStm))
{
//xnameSpace.Add("ns0", "urn:ihe:iti:xds-b:2007");
serializer.WriteObject(xw, oReq);
memStm.Position = 0;
xReq.Load(memStm);
}
}
但我得到了一个错误:“有一个未关闭的文本字符串。第1行,位置12284。”在“xReq.Load(memStm);”行
我正在使用UTF-8编码,但我已经读到这是“XmlSerializerNamespaces”的默认值
有什么问题吗?在读取之前,您必须刷新
XmlWriter
:
serializer.WriteObject(xw, oReq);
// Make sure all the XML has been written to the stream.
xw.Flush();
memStm.Position = 0;
xReq.Load(memStm);
否则,序列化XML的最后一部分不会写入流,在您的例子中,这会导致开始引用而没有结束引用。在我的例子中,程序仍然保留XML文件,因此会出现此错误。结束节目解决了问题