的XDocument防止无效charachters [英] XDocument prevent invalid charachters

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

问题描述

我使用的XDocument保持一个形式的数据库。该数据库由注册chatterbots,我只是有很多僵尸与属性,比如用户名,所有者,这样的节点。不过,偶尔有些聪明的家伙决定作出一个机器人用的非常的奇怪字符作为属性之一。这使得因为数据库无法完全保存,因为它便会立即停止写入文件,因为它击中无效字符的XDocument类系列抛出一个异常,每当节点被读取,一个非常大的问题。



我的问题是这个 - 有没有是一样的东西 XSomething.IsValidString(字符串s)一个简单的方法,这样我就可以省略违规数据?我的数据库是不是官方的,只是个人使用,所以它不是必要的,我有不好的数据。



一些代码,我使用(变量文件是的XDocument):结果
要节省:结果
file.Save(Path.Combine(环境.CurrentDirectory,bots.xml));



要加载(检查后,如果 File.Exists() 等等等等):结果
文件= XDocument.Load(Path.Combine(Environment.CurrentDirectorybots.xml));



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

  file.Root.Add(新的XElement(机器人,
新XAttribute(用户名,botusername),
新XAttribute(类型,类型),
新XAttribute(botversion,botversion),
新XAttribute(bdsversion,bdsversion),
新XAttribute(所有者,拥有者),
新XAttribute(触发,触发) ));



原谅我缺乏正确的XML技术,我只是开始。如果有一个 XSomething.IsValidString(字符串s)的方法,我的XML不是多么可怕的是什么我问的是。


$ B $ :b

好吧,我刚刚得到了再一次例外,这里是确切的消息和堆栈跟踪

  System.ArgumentException: '',十六进制值值为0x07,是无效字符。 
在System.Xml.XmlUtf8RawTextWriter.InvalidXmlChar(CH的Int32,BYTE * pDst,布尔entitize)
在System.Xml.XmlUtf8RawTextWriter.WriteAttributeTextBlock(字符* PSRC,字符* pSrcEnd)
。在系统.Xml.XmlUtf8RawTextWriter.WriteString(字符串文本)
在System.Xml.XmlUtf8RawTextWriterIndent.WriteString(字符串文本)
在System.Xml.XmlWellFormedWriter.WriteString(字符串文本)
在的System.Xml .XmlWriter.WriteAttributeString(前缀字符串,字符串的localName,NS字符串,字符串值)
在System.Xml.Linq.ElementWriter.WriteStartElement(的XElement E)
在System.Xml.Linq.ElementWriter.WriteElement(的XElement E)
在System.Xml.Linq.XElement.WriteTo(XmlWriter的作家)
在System.Xml.Linq.XContainer.WriteContentTo(XmlWriter的作家)
在System.Xml.Linq的。 XDocument.WriteTo(XmlWriter的作家)
在System.Xml.Linq.XDocument.Save(字符串文件名,SaveOptions选项)
在System.Xml.Linq.XDocument.Save(字符串文件名)
在省略/ *我的代码堆栈跟踪* /


解决方案

尝试改变file.Save线以下代码:

  XmlWriterSettings设置=新XmlWriterSettings(); 
settings.CheckCharacters = FALSE;
XmlWriter的作家= XmlWriter.Create(Path.Combine(Environment.CurrentDirectorybots.xml),设置);
file.Save(作家);

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


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.

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.

Some code that I am using (the variable file is the XDocument):
To save:
file.Save(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)));

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

source: http://sartorialsolutions.wordpress.com/page/2/

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

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