XDocument防止无效字符 [英] XDocument prevent invalid characters

查看:110
本文介绍了XDocument防止无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XDocument来保留某种数据库.该数据库由注册的chatterbots组成,我只是拥有许多具有属性的"bot"节点,例如用户名",所有者"等.但是,偶尔会有一些聪明的人决定将具有非常角色的机器人作为其中一项特性.这使得XDocument类系列在每次读取该节点时都会引发异常,这是一个非常大的问题,因为一旦遇到无效字符,数据库就会停止写入文件,从而导致数据库无法完全保存.

I am using XDocument to keep a sort of database. This database consists of registered chatterbots, and I simply have many "bot" nodes with attributes such as "username", "owner", and such. However, occasionally some smart guy decides to make a bot with a very strange character as one of the properties. This makes the XDocument class series throw an exception whenever that node is read, a very large problem because the database fails to save completely as it stops writing to the file as soon as it hits the invalid character.

我的问题是-是否有一个类似于XSomething.IsValidString(string s)的简单方法,因此我可以省略有问题的数据?我的数据库不是官方数据库,而是个人使用数据库,因此不一定要包含不良数据.

My question is this- Is there a simple method that is something like XSomething.IsValidString(string s), so I can just omit the offending data? My database is not the official one, just a personal use, so it is not imperative that I include the bad data.

我正在使用的某些代码(变量file是XDocument):
保存:
file.Save(Path.Combine(Environment.CurrentDirectory, "bots.xml"));

Some code that I am using (the variable file is the XDocument):
To save:
file.Save(Path.Combine(Environment.CurrentDirectory, "bots.xml"));

要加载(在检查File.Exists()等之后):
file = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "bots.xml"));

To load (after checking if File.Exists() etc etc):
file = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "bots.xml"));

要添加到数据库中(变量都是字符串):

To add to the database (variables are all strings):

            file.Root.Add(new XElement("bot",
                new XAttribute("username", botusername),
                new XAttribute("type", type),
                new XAttribute("botversion", botversion),
                new XAttribute("bdsversion", bdsversion),
                new XAttribute("owner", owner),
                new XAttribute("trigger", trigger)));

请原谅我缺乏适当的XML技术,我才刚刚开始.我要问的是是否有XSomething.IsValidString(string s)方法,而不是XML多么糟糕.

Pardon my lack of proper XML techniques, I'm just starting. What I'm asking is if there is a XSomething.IsValidString(string s) method, not how terrible my XML is.

好吧,我又得到了一个例外,这是确切的消息和堆栈跟踪.

Ok, I just got the exception again, here is the exact message and stack trace.

System.ArgumentException: '', hexadecimal value 0x07, is an invalid character.
at System.Xml.XmlUtf8RawTextWriter.InvalidXmlChar(Int32 ch, Byte* pDst, Boolean entitize)
at System.Xml.XmlUtf8RawTextWriter.WriteAttributeTextBlock(Char* pSrc, Char* pSrcEnd)
at System.Xml.XmlUtf8RawTextWriter.WriteString(String text)
at System.Xml.XmlUtf8RawTextWriterIndent.WriteString(String text)
at System.Xml.XmlWellFormedWriter.WriteString(String text)
at System.Xml.XmlWriter.WriteAttributeString(String prefix, String localName, String ns, String value)
at System.Xml.Linq.ElementWriter.WriteStartElement(XElement e)
at System.Xml.Linq.ElementWriter.WriteElement(XElement e)
at System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
at System.Xml.Linq.XContainer.WriteContentTo(XmlWriter writer)
at System.Xml.Linq.XDocument.WriteTo(XmlWriter writer)
at System.Xml.Linq.XDocument.Save(String fileName, SaveOptions options)
at System.Xml.Linq.XDocument.Save(String fileName)
at /* my code stack trace omitted */

推荐答案

尝试更改文件.保存以下代码行:

Try changing the file.Save line for the following code:

XmlWriterSettings settings = new XmlWriterSettings();
settings.CheckCharacters = false;
XmlWriter writer = XmlWriter.Create(Path.Combine(Environment.CurrentDirectory, "bots.xml"), settings);
file.Save(writer);

来源: http://sartorialsolutions.wordpress.com/page/2/

这篇关于XDocument防止无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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