XElement& UTF-8问题 [英] XElement & UTF-8 Issue

查看:165
本文介绍了XElement& UTF-8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Web服务(.asmx,而不是.svc),它通过HTTP POST接受一个字符串。它接受的字符串是xml信息集,然后我通过XElement.Parse进行解析。解析成XElement实例后,我将一个节点添加到实例中的一个元素。


我遇到的问题是,如果表示xml信息集的字符串通过然后由于某种原因,我向元素XElement添加一个节点会抛出一个异常,例如"'',十六进制值0x06,是一个无效字符。第1行,第40位。"
我得到了大量的0x(*)错误。如果我不尝试向XElement添加节点,那么一切都很好。以下是我添加元素的方法:

  var prospect = doc.Element(" prospect"); 
var provider = prospect.Element(" provider");

provider.Add(new XElement(" id",
new XAttribute(" reservation-code",
reservationCode)
));

我应该在某处进行某种字符串转换吗?


Blades不需要重新加载...

解决方案

在XML(1.0)中不允许该字符与其他控制字符一样。但据我所知,当您使用新的XElement / XAttribute创建具有此类内容的节点时,LINQ to XML实现不会引发任何错误,而是在您序列化
时(例如,通过调用ToString()或使用Save())解析XElement或XDocument上的方法)或解析时(例如使用Load或Parse方法)。因此,我有点惊讶你说你在Add call上得到了错误。


你能否发布确切的异常类型和你得到的堆栈跟踪?


I have a .NET Web Service(.asmx, not .svc) that accepts a string via HTTP POST. The strings it accepts are xml infosets I then parse via XElement.Parse. Once parsed into an XElement instance, I add a node to one of the elements within the instance.

The problem I'm having is that if a string representing an xml infoset comes through with then for some reason, me adding a node to the element XElement throws an exception such as "' ', hexadecimal value 0x06, is an invalid character. Line 1, position 40.". I get a wide array of 0x(*) errors thrown. If I don't attempt to add nodes to the XElement, everythings fine. Here's how I'm adding the element:

var prospect = doc.Element("prospect");
var provider = prospect.Element("provider");

provider.Add(new XElement("id",
  new XAttribute("reservation-code",
  reservationCode)
));

Is there some sort of string conversion I ought to be doing somewhere?


Blades Don't Need Reloading...

解决方案

That character, like other control characters, is not allowed in XML (1.0). As far as I know however the LINQ to XML implementation does not throw any error when you create nodes with such content with new XElement/XAttribute but rather when you serialize (e.g. by calling ToString() or using the Save() method on an XElement or XDocument) or when you parse (e.g. with Load or Parse methods). Thus I am a bit astonished that you say you get the error on the Add call.

Can you please post the exact type of the exception and the stack trace you get?


这篇关于XElement& UTF-8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆