C# 中不区分大小写的 XML 解析器
我知道您对 XML 所做的一切都区分大小写。
然而,现在我发现自己处于一种情况,如果我以某种方式使 xml 名称/属性识别不区分大小写,我正在编写的软件会产生更少的错误。不区分大小写的 XPath 将是上帝派来的。
在 C# 中是否有一种简单的方法/库可以做到这一点?
Everything you do with XML is case sensitive, I know that.
However, right now I find myself in a situation, where the software I'm writing would yield much fewer errors if I somehow made xml name/attribute recognition case insensitive. Case insensitive XPath would be a god sent.
Is there an easy way/library to do that in c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个 XMl 文档可以有两个不同的元素,分别命名为:
MyName
和myName
——它们的目的是不同的。将它们转换/视为相同的名称是一个错误,可能会产生严重后果。如果上述情况并非如此,那么这里有一个更精确的解决方案,使用 XSLT 将文档处理为仅包含小写字母的文档元素名称和小写属性名称:
当此转换应用于任何 XML 文档时,例如这个:
生成所需的正确结果(元素和属性名称转换为小写)强>:
一旦文档转换为您想要的形式,然后您可以对转换后的文档进行任何所需的处理。
An XMl document can have two different elements named respectively:
MyName
andmyName
-- that are intended to be different. Converting/treating them as the same name is an error that can have gross consequences.In case the above is not the case, then here is a more precise solution, using XSLT to process the document into one that only has lowercase element names and lowercase attribute names:
when this transformation is applied on any XML document, for example this one:
the wanted, correct result (element and attribute names converted to lowercase) is produced:
Once the document is converted to your desired form, then you can perform any desired processing on the converted document.
您可以创建不区分大小写的方法(可用性扩展),例如:
You can create case-insensitive methods (extensions for usability), e.g.:
XML 是文本。在加载到您正在使用的任何解析器之前只需
ToLower
即可。只要您不必针对架构进行验证并且不介意值全部小写,那么这应该可以正常工作。
事实上,任何 XML 解析器都将区分大小写。如果不是,它就不是 XML 解析器。
XML is text. Just
ToLower
it before loading to whatever parser you are using.So long as you don't have to validate against a schema and don't mind the values being all lower case, this should work just fine.
The fact is that any XML parser will be case sensitive. If it were not, it wouldn't be an XML parser.
我使用另一种解决方案。人们想要这样做的原因是因为您不想在属性中也复制类文件中的属性名称。所以我要做的就是向所有属性添加一个自定义属性:
这样 XML 序列化程序就可以将小写属性映射到 CamelCased 类。
类上的属性仍然有一个装饰器来表示有些东西是不同的,但是您没有用名称标记每个属性的开销:
I use another solution. The reason people want this is because you don't want to duplicate the name of the property in the class file in an attribute as well. So what I do is add a custom attribute to all properties:
This way the XML serializer can map lower case properties to CamelCased classes.
The properties on the classes still have a decorator that says that something is different, but you don't have the overhead of marking every property with a name:
我首先使用
SAX
解析将所有标签和属性名称转换为小写,保持值不变,即。使用 XmlTextReader。I would start by converting all tags and attribute names to lowercase, leaving values untouched, by using
SAX
parsing, ie. withXmlTextReader
.