2013-12-09 16 views
0

我有這個類從web服務生成:有一個未封閉的文字字符串。 1號線,位置12284

[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.」 on「xReq.Load(memStm);」線。

我使用UTF-8編碼,但我讀過,是「XmlSerializerNamespaces」的默認..

什麼可以是問題?

回答

1

您可以從中讀取之前沖洗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的最後部分是不是沒有結束引號寫在一個開引號的情況下,結果流哪個。

0

在我的情況下,程序仍然堅持XML文件,因此得到這個錯誤。結束程序解決了問題。

相關問題