在客户端的服务器+ GWT上使用.NET c#,我有一个Web窗体,它接受用户输入,然后构建一个XML字符串并将其存储在数据库中。然后我需要从数据库中读取它,通过tcp将它发送到手持设备,并将其解析为XElement。一切运作良好,直到您从Word中复制和粘贴文本或在这种情况下脱颖而出,当我尝试这样做:c#解析包含HTML特殊字符的字符串XElement

XElement.parse(str);

它抛出一个异常:

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

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

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

{ WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_ENTRY_EXIT, “++调用({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'循环。 –

2011-03-29 10:03:41

+0

好的,我已经粘贴了现在在这里完成这项工作的方法。从第70行开始,你可以看到我如何建立字符串。 –

2011-03-29 10:17:21

+0

你从WebServicesClient.Debug.DebugMessage(WebServicesClient.Debug.MASK_RPC_CALL,“RPC(Method = {0},replyContent ='{1}')”,methodName,replyContent)获得了什么输出结果?' –

2011-03-29 10:28:29

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

  1. url 传参时包含或者#号时当成参数解析的问题

    url 传参时包含&或者#号时当成参数解析的问题 当url地址传递参数时,值中一个含有&符号时可能会出现解析错误的情况,最好的解决办法就是将&符号进行replace成其他格式字 ...

  2. js---原生js,url传参

    js 通过url传参 模拟: 页面一(A.html)登录 传递参数 用户名 Uname 密码 Upassword 页面二(B.html)接收参数 页面一(A.html) <body>< ...

  3. Java URL传参中文乱码问题

    2019独角兽企业重金招聘Python工程师标准>>> 我们经常会遇到这样的场景:在URL中传递参数,如果该参数为中文,如果设置不当,会出现乱码问题. URL传参所使用的编码为服务器 ...

  4. python get请求 url传参_requests的get请求url参数、url重定向处理及cookies

    需求:在百度搜索www.python66.com,然后将搜索结果保存到文件bd_python66.html 百度搜索的url:https://www.baidu.com/s?wd=搜索词 params ...

  5. 关于url传参中文乱码问题

    之前都一直很不了解中文编码得问题,之前在做项目中没碰到那么头痛的问题.所以一直没有了解中文乱码的问题. 问题描述: 地址: http://localhost:8080/sun-government/c ...

  6. 微信小程序页面跳转,url传参参数丢失问题

    微信小程序页面跳转,url传参参数丢失问题 // pages/order/purchase/index.js// 跳转到采购订单详情toPurchaseOrderDetail(e) {// conso ...

  7. 记一次vue踩坑 this.$router.back()在ios失效,试了各种返回,最后发现是因为url传参的原因,去掉参数就可以了。。

    因为之前有项目也用过类型的,我就找不同,发现之前项目的打包完直接用的....index.html访问 现在的项目 用的....index.html#/访问的 --------------------- ...

  8. URL传参时 从URL中获取中文参数的方法

    利用url传参时如果url中的参数是中文时因为编码类型不同在页面中获取会出现乱码 使用此方法能获取url中的参数值 并解决乱码问题 调用时直接 GetUrlByParamName("参数名& ...

  9. vue前后端aes url传参解密再解base64编码

    需求:我们公司做机票订购服务,有自己的平台的订单,也有第三方网站向我们网站导入的用户订单,第三方导入的是通过url传参的方式导入.url会携带用户的相关信息. url里面的参数是经过aes加密和bas ...

最新文章

  1. windows server 中,Tomcat9 配置
  2. JavaXml教程(十)XML作为属性文件使用
  3. 2017年,这两个大数据岗位一定会火!
  4. 中奖人js滚动效果_js使用transition效果实现无缝滚动
  5. 微课|中学生可以这样学Python(6.3节):变量作用域
  6. 一步步编写avalon组件03:切换卡组件
  7. 国标:PAAS应用程序管理要求
  8. 计算机护眼模式怎么设置的,教你如何开启电脑的护眼模式
  9. Flutter热重载原理探索调试
  10. FPN(Feature Pyramid Networks)学习笔记
  11. ClickHouse 之 FORMAT 应用
  12. linux系统查看u盘容量,在LINUX系统中编程查询U盘或软盘格式信息:总容量、空余容量、已用容量等。...
  13. PCIe扫盲——PCIe错误源详解(一)
  14. 圆满落幕!回顾 eBPF 技术的发展与挑战
  15. Ubuntu 18.04 ———(Intel RealSense D435i)运行VINS-Mono
  16. ROS-3DSLAM(十一)lvi-sam源代码阅读9
  17. top(linux)——FIELDS/Columns含义
  18. 单声道音频播放出立体声效果
  19. 编译原理 --- 高级程序设计语言概述
  20. 35岁程序员职业危机?那45-50岁的程序员都在干什么?

热门文章

  1. Google强化学习框架SEED RL环境部署
  2. php新建文件在指定目录下,PHP在获取指定目录下的目录,在获取的目录下面再创建文件,多平台...
  3. tcp unity 图片_Unity 简易聊天室(基于TCP)(2)
  4. 查看grafana版本_使用 Prometheus 与 Grafana 为 Kubernetes 集群建立监控与警报机制
  5. pythonrequests证书_python requests证书问题解决
  6. linux qt手册,明远智睿I.MX6 Linux-4.1.15 QT5 程序编译手册
  7. 谷歌浏览器怎么查看网页源代码 Chrome浏览器网页源代码查看技巧分享
  8. Android如何回调编码后的音视频数据
  9. centos mysql_CentOS MySQL数据库备份工具mysqldump介绍
  10. SpringBoot使用Slf4j+Log4j2完成项目的日志记录