【发布时间】:2013-03-12 18:48:15
【问题描述】:
我正在开发一个通过 SOAP 从 Web 服务下载 XML 文件的 C# 客户端。对于托管在服务上的一些较旧的记录,XML 显然会遇到埋在其中某处的 0x14,这会引发“无效的空白字符”异常。我正在使用 Linq 将 XML 转储到文件中。有什么方法可以指示 Linq 在不丢失 XML 其余部分的情况下处理无效字符?
编辑:
这是我目前将 XML 放入文件的代码:
XDocument c =
new XDocument(
new XElement(nameSpace + "getCitationsResponse",
new XAttribute(XNamespace.Xmlns + "ns1", nameSpace),
new XElement("list",
record.reportDateSpecified ? new XElement("reportDate", record.reportDate) : null,
new XElement("reportType", record.reportType),
new XElement("title", record.title),
new XElement("projectNumber", record.projectNumber),
new XElement("author", record.author),
new XElement("abstract", record.@abstract),
new XElement("numPages", record.numPages),
record.isDataTypeSpecified ? new XElement("isDataType", record.isRestrictedData) : null,
new XElement("comments", record.comments),
new XElement("attachments", from a in record.attachments
select new XElement("list",
new XElement("id", a.id),
new XElement("filePath", a.filePath),
new XElement("type", a.type)))));
出于通常的原因,我不得不删除其中的一些内容,但我删除的内容与此处显示的内容相同。
我在发布之前使用了 SoapUI,看看我是否可以找出缺陷所在,但我在 SoapUI 中没有看到任何东西,它本身也不会产生错误。 p>
编辑#2:
这是确切的错误消息和堆栈跟踪。让我想知道我是否真的可以对此做点什么,或者我是否只需要做一些事情来记录哪些记录具有无效字符并尝试使用 SoapUI 手动将它们拉下来。
Invalid white space character (0x14) in text to output
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at Downloader.WebService.ApiService.getRecords(String username, String[] ids)
at Downloader.Central.RecordLoop(ApiService svc, Int32 offset, String username)
getRecords 是 wsdl 生成的 API 调用,而 RecordLoop 是我编写的一个递归函数,用于处理通过 API 调用迭代以查找更新的记录并将它们推送到我已经发布的 Linq 函数。
【问题讨论】:
-
能否提供一些示例数据给我们?
-
你可以在这里查看这个解决方案:seattlesoftware.wordpress.com/2008/09/11/…
-
如果它带有无效字节,那么严格来说它实际上不是XML......只是说......
-
我添加了 Linq 代码和更多解释。我去看看那个链接。
-
如果文档以字符串形式出现,您可以只使用
string.Replace错误字符。如果它以 XML 文档的形式出现,您可能会将其转换为字符串,执行Replace操作,然后重新创建文档。