c# - 使用 XElement 处理无效的 XML 字符

标签 c# xml xml-parsing xelement xmlconvert

我有一个返回 XElement 对象的 C# API。这个 XElement 对象是通过看起来像这样的代码构造的 -

string invalidXML = "a \v\f\0";    
XElement fe = new XElement("Data", invalidXML);
Console.WriteLine(fe);

通过观察,我知道当试图将无效的 XML 字符传递给上面的 XElement 构造函数时,将引发 System.Argument 异常。

事实证明,当传递具有 InvalidXML 字符的字符串时,XElement 不会抛出错误。如果您尝试通过 Console.WriteLine(fe) 打印 XElement,那么您会从 XMLWriter-

System.ArgumentException: '', hexadecimal value 0x0B, is an invalid character.
   at System.Xml.XmlEncodedRawTextWriter.InvalidXmlChar(Int32 ch, Char* pDst, Boolean entitize)
   at System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
   at System.Xml.XmlEncodedRawTextWriter.WriteString(String text)
   at System.Xml.XmlEncodedRawTextWriterIndent.WriteString(String text)
   at System.Xml.XmlWellFormedWriter.WriteString(String text)
   at System.Xml.Linq.ElementWriter.WriteElement(XElement e)
   at System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
   at System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
   at System.Xml.Linq.XNode.ToString()
   at System.IO.TextWriter.WriteLine(Object value)
   at System.IO.TextWriter.SyncTextWriter.WriteLine(Object value)
   at System.Console.WriteLine(Object value)
   at TestLoggingForUNIT.Program.Main(String[] args) in C:\Users\shivanshu\source\repos\TestLoggingForUNIT\TestLoggingForUNIT\Program.cs:line 29

对我来说,XElement 本身似乎不做任何验证。在 .NET 中,当它被打印/序列化时,会在内部调用 XML 编写器,这就是抛出异常的时候。

我的问题是,XElement 总是保证在传递无效的 XML 字符时抛出异常。

换句话说,我是否需要检查我传递的字符串是否存在无效的 XML 字符?使用像 XmlConvert.IsXmlChar(string) 这样的东西?

我查看了下面的链接,但找不到对我的问题的满意答案-

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/valid-content-of-xelement-and-xdocument-objects3

最佳答案

它是XmlWriter那是验证正在写入有效字符。在官方文档中,相关的XmlWriter配置在Data Conformance部分有描述:

Data conformance

An XML writer uses two properties from the XmlWriterSettings class to check for data conformance:

The CheckCharacters property instructs the XML writer to check characters and throw an XmlException exception if any characters are outside the legal range, as defined by the W3C.

The ConformanceLevel property configures the XML writer to check that the stream being written complies with the rules for a well-formed XML 1.0 document or document fragment, as defined by the W3C. The three conformance levels are described in the following table. The default is Document. For details, see the XmlWriterSettings.ConformanceLevel property and the System.Xml.ConformanceLevel enumeration.

是的,将 CheckCharacters 标志设置为 true,它将保证在遇到非法字符时抛出异常。

如果你想允许写入无效字符,CheckCharacters标志可以在 XmlWriterSettings 中设置为 false对于您的 XmlWriter,这将防止抛出异常。通常,XmlWriter 会将保留字符编码为字符实体(例如 <&lt; )。此外,如果标志设置为 false,XmlWriter 会将非法字符转义为数字字符实体(例如 \f&#xC; )以生成符合 XML 规范的文本。

关于c# - 使用 XElement 处理无效的 XML 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032245/

相关文章:

xml - XSD 重用complexType 元素

sql-server - SQL Server XML 数据类型和 QUOTED_IDENTIFIER

c++ - 使用 RapidXML 解析

c# - Enumerable.Range - 什么时候使用它有意义?

c# - 日期问题的正则表达式

c# - Xamarin.Forms 绑定(bind)有效,但文本未显示

python - 使用 Python 从 XML 中提取值

c# - 组织 c# 项目帮助程序或实用程序类

xml - SoapUI 在模拟服务脚本中获取请求参数

Java 斯塔克斯 : Invalid byte 2 of 3-byte UTF-8 sequence