[英]XmlDocument to read XML file, passed as SOAP message body
我正在使用XmlDocument讀取XML文檔的內容,然后將這些內容傳遞到Web服務請求的SOAP消息正文中。 但是,在調用中實際傳遞的數據(從XML文檔中讀取)中,<和>符號被替換為
> and <
導致接收端出現問題。
我想念什么?
這是我的代碼片段:
string filePath = FileList.SelectedItem.Value;
string doc = File.ReadAllText(filePath);
XmlDocument xDoc = new XmlDocument { PreserveWhitespace = true };
xDoc.LoadXml(doc);
MyService.TransactionRequest request = new MyService.TransactionRequest { message = xDoc.OuterXml };
MyService.TransactionClient client = new MyService.TransactionClient();
client.ProcessTransaction(request);
感謝您提供的所有幫助。
您可能希望將消息的內容包裝在CDATA構造中,該構造(敲敲木頭)可以防止SOAP消息編碼器轉換字符串中的XML標簽。
string wrapped = "<![CDATA[" + xDoc.OuterXml + "]]>";
TransactionRequest request = new MyService.TransactionRequest { message = wrapped };
如果您的xDoc本身可能包含CDATA節,則您需要做更多的工作,因為CDATA節不能嵌套。 不過,有一個簡單的解決方法。 只需將要包裝的字符串中所有出現的"]]>"
替換為"]]]]><![CDATA[>"
。
string toWrap = xDoc.OuterXml.Replace("]]>", "]]]]><![CDATA[>");
string wrapped = "<![CDATA[" + toWrap + "]]>";
TransactionRequest request = new MyService.TransactionRequest { message = wrapped };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.