gpt4 book ai didi

c# 将包含 HTML 特殊字符的字符串解析为 XElement

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:24 24 4
gpt4 key购买 nike

在服务器端使用 .NET c# + 在客户端使用 GWT,我有一个 Web 表单,它接受用户输入,然后我从中构造一个 XML 字符串并将其存储在数据库中。然后我需要从数据库中读回它,通过 tcp 将它发送到手持设备并将它解析为 XElement。一切正常,直到您从 word 或 excel 中复制和粘贴文本,在这种情况下,当我尝试这样做时:

XElement.parse(str);

它抛出一个异常:

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

会导致此问题的示例字符是右撇号字符 (0x2019)。现在可能会有一大堆特殊字符可能从 excel/word 等复制粘贴。处理这个问题的最佳方法是什么?以下是我如何从流构造字符串:

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

{ WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_ENTRY_EXIT, "++Call({0}, ...)", methodName);

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

最佳答案

System.Text.Encoding.UTF8 因为它看起来像 Excel 使用 utf。转换为您喜欢的编码。

关于c# 将包含 HTML 特殊字符的字符串解析为 XElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com