日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# url传参不能包含html标签,c#解析包含HTML特殊字符的字符串XElement

發(fā)布時間:2025/3/12 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# url传参不能包含html标签,c#解析包含HTML特殊字符的字符串XElement 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在客戶端的服務(wù)器+ GWT上使用.NET c#,我有一個Web窗體,它接受用戶輸入,然后構(gòu)建一個XML字符串并將其存儲在數(shù)據(jù)庫中。然后我需要從數(shù)據(jù)庫中讀取它,通過tcp將它發(fā)送到手持設(shè)備,并將其解析為XElement。一切運(yùn)作良好,直到您從Word中復(fù)制和粘貼文本或在這種情況下脫穎而出,當(dāng)我嘗試這樣做:c#解析包含HTML特殊字符的字符串XElement

XElement.parse(str);

它拋出一個異常:

'.', hexadecimal value 0x00, is an invalid character. Line 132, position 111.

例字符,將導(dǎo)致此問題是正確的撇號字符(0x2019)。現(xiàn)在可能會有一大堆特殊字符可能從excel/word復(fù)制粘貼等。處理此問題的最佳方法是什么?下面是我如何構(gòu)建從流串:

protected CallResult callUsingSocketClass(string methodName, params Action[] addParameters)

{ WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_ENTRY_EXIT, “++調(diào)用({0},...)”,方法名);

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

CallParameters parameters = new CallParameters(this, methodName);

foreach (var addParameter in addParameters)

{

addParameter(parameters);

}

string post = this.CreatePost(parameters);

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC(Method={0}, host={1}, port={2}, post='{3}')", methodName, this.Host, this.Port, post);

byte[] postBytes = Encoding.UTF8.GetBytes(post);

//

// Send the request and wait for the reply.

//

char[] replyContentChars = null;

for (int attempts = 0; attempts < 3; attempts++)

{

Socket socket = null;

try

{

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - creating socket for RPC call...");

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

string hostID = this.Host;

if (this.HostIPAddress != null)

{

hostID = this.HostIPAddress;

}

using (socket = this.connectToServer(hostID, this.Port))

{

if (socket == null)

{

return null;

}

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - socket created!");

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - communicating with server...");

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - writing post...");

this.sendDataToServer(socket, postBytes);

int replyLength = -1;

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - reading reply...");

using (var reader = this.receiveStreamFromServer(socket))

{

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

socket.Close();

socket = null;

return null;

}

}

for (; ;)

{

string lineRaw = reader.ReadLine().Trim();

string line = lineRaw.ToLowerInvariant();

if (line.StartsWith("content-length:"))

{

replyLength = int.Parse(line.Substring(15));

}

else if (line == "")

{

if (replyLength < 0)

{

throw new InvalidOperationException("Reply hasn't specified content-length");

}

break;

}

else

{

if (this.CookieJar != null)

{

this.CookieJar.ProcessFromServer(lineRaw);

}

}

}

// Content starts here

replyContentChars = new char[replyLength];

int replyRecv = 0;

do

{

int charsRecv = reader.Read(replyContentChars, replyRecv, replyLength - replyRecv);

if (charsRecv <= 0)

{

break;

}

replyRecv += charsRecv;

} while (replyRecv < replyLength);

//int charsRecv = reader.Read(replyContentChars, 0, replyLength);

if (replyRecv != replyLength)

{

untime.Logger.Logger.Error("Web Service call '{0}' received {1} bytes, header indicated {2} bytes", methodName, replyRecv, replyLength);

throw new InvalidOperationException(String.Format("Have not received all of reply data - received {0} bytes, expected {1}", replyRecv, replyLength));

}

}

socket.Close();

socket = null;

}

}

catch (Exception e)

{

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC - exception thrown - {0} [{1},{2}]", e.Message, e.Source, e.StackTrace);

}

finally

{

if (socket != null)

{

socket.Close();

socket = null;

}

}

if (replyContentChars != null)

{

break;

}

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

}

//

// Verify that data has been received.

//

if (replyContentChars == null)

{

return null;

}

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

//

// Process the received data.

//

string replyContent = new string(replyContentChars);

WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL, "RPC(Method={0}, replyContent='{1}')", methodName, replyContent);

XElement xReplyContent = XElement.Parse(replyContent);

var xReplyBody = xReplyContent.Element(nsSoap + "Body");

var xFault = xReplyBody.Element(nsSoap + "Fault");

if (xFault != null)

{

// Something has gone wrong on the server

var xFaultCode = xFault.Element(nsSoap + "Code");

var xFaultReason = xFault.Element(nsSoap + "Reason");

untime.Logger.Logger.Error("Web Service call to method '{0}' failed: Code='{1}', Reason='{2}'", methodName, (string)xFaultCode, (string)xFaultReason);

string faultCode = (string)xFaultCode;

var codeParts = faultCode.Split(':');

XmlQualifiedName xmlQualifiedName;

if (codeParts.Length == 2)

{

xmlQualifiedName = new XmlQualifiedName(codeParts[1], codeParts[0]);

}

else

{

xmlQualifiedName = new XmlQualifiedName(faultCode);

}

throw new SoapException((string)xFaultReason, xmlQualifiedName);

}

var xResponse = xReplyBody.Element(this.nsArgs + (methodName + "Response"));

var xResult = xResponse.Element(this.nsArgs + (methodName + "Result"));

if (this.OnTransferring != null)

{

if (!this.OnTransferring())

{

return null;

}

}

var result = new CallResult(xResult);

return result;

}

2011-03-28

Shahid

+0

代碼不完整。 'replyLength'在哪里定義?在這個例子中你有一個無盡的'for'循環(huán)。 –

2011-03-29 10:03:41

+0

好的,我已經(jīng)粘貼了現(xiàn)在在這里完成這項工作的方法。從第70行開始,你可以看到我如何建立字符串。 –

2011-03-29 10:17:21

+0

你從WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL,“RPC(Method = {0},replyContent ='{1}')”,methodName,replyContent)獲得了什么輸出結(jié)果?' –

2011-03-29 10:28:29

總結(jié)

以上是生活随笔為你收集整理的c# url传参不能包含html标签,c#解析包含HTML特殊字符的字符串XElement的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。