【问题标题】:XDocument prevent invalid charactersXDocument 防止无效字符
【发布时间】:2012-04-20 21:06:20
【问题描述】:

我正在使用 XDocument 来保存某种数据库。该数据库由注册的聊天机器人组成,我只是有许多具有“用户名”、“所有者”等属性的“机器人”节点。然而,偶尔一些聪明的人会决定制作一个具有 非常 奇怪字符作为属性之一的机器人。这使得 XDocument 类系列在读取该节点时抛出异常,这是一个非常大的问题,因为数据库无法完全保存,因为它一旦遇到无效字符就停止写入文件。

我的问题是——有没有像XSomething.IsValidString(string s) 这样的简单方法,所以我可以省略违规数据?我的数据库不是官方的,只是个人使用,所以我没有必要包含坏数据。

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

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

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

            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 有多糟糕。

好的,我又遇到了异常,这是确切的消息和堆栈跟踪。

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 */

【问题讨论】:

标签: c# xml linq-to-xml


【解决方案1】:

尝试更改以下代码的 file.Save 行:

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/

【讨论】:

  • 这看起来可行,谢谢。有问题的机器人现在不在线,所以我无法测试它,但非常感谢。看起来 0x07 是唯一的无效字符。如果这是真的,我可能会做一个 string.Replace("\a", "");,但你的解决方案对于那些需要存储 \a 的人来说更好。
【解决方案2】:

首先你可以检查你的XML文件是否以正确的编码保存?我通常将 xml 文件保存为 UTF8,您可以在 xml 标头中声明编码

<?xml version="1.0" encoding="UTF-8"?>

当然,您的 xml 正文必须符合 xml 标准。这是一篇关于它的好文章

http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx

【讨论】:

  • 是的,我的标题正是那一行,除了它的“utf-8”而不是“UTF-8”。不过,不应该有所作为(我认为)。我查看了十六进制值 0x07,它显然是“主板哔声”,我认为代码中的 \a。我可以只检查这个字符,可能还有更多我不知道的无效字符。
【解决方案3】:

从 .NET 4 开始,您可以使用 XmlConvert.VerifyXmlChars(string content)。如果传递的字符串不被接受,这将抛出异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多