簡體   English   中英

XmlDocument讀取XML文件,作為SOAP消息主體傳遞

[英]XmlDocument to read XML file, passed as SOAP message body

我正在使用XmlDocument讀取XML文檔的內容,然后將這些內容傳遞到Web服務請求的SOAP消息正文中。 但是,在調用中實際傳遞的數據(從XML文檔中讀取)中,<和>符號被替換為

&gt; and &lt; 

導致接收端出現問題。

我想念什么?

這是我的代碼片段:

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

參見http://en.wikipedia.org/wiki/CDATA

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM