覆盖或使用LINQ在C#中忽略未申报的实体 [英] Overriding or ignoring undeclared entities in C# using LINQ

查看:132
本文介绍了覆盖或使用LINQ在C#中忽略未申报的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,通过寻找使用LINQ在XML文件中某些事物运行的小工具。它相当快,并很好地处理它们的大量收集。但是,一定批处理文件中约20%无法被读取和被跳过,因为作为&放度数符号的存在失败;度; 中的文件。这是引用了未声明实体'度'。一个前面的问题是怎么回事。

I have a little utility that runs through looking for certain things in XML files using LINQ. It processes a MASSIVE collection of them rather quickly and nicely. However, about 20% of a certain batch of files fail to be read and are skipped, failing because of the degree symbol's presence as ° in the files. This is the "Reference to undeclared entity 'deg'." a previous question was about.

在上一个问题所提供的解决方案不能直接应用在这里。我不能随意去走一走修改的文件,使它们的副本和替换的实例或拷贝插入标签似乎效率不高。什么是去获得LINQ忽略未申报的实体,这绝对对我的程序在做什么反正没有关系的最佳方式?还是有可能要事先喂一些实体声明得到一个XDocument.Load的好办法?

The solutions offered in the previous question cannot be directly applied here. I am not at liberty to go around modifying the files, and making copies of them and replacing instances or inserting tags in the copies seems inefficient. What would be the best way to go about getting LINQ to ignore the undeclared entities, which have absolutely no bearing on what my program does anyway? Or is there perhaps a good way of getting an XDocument.Load to be fed some entity declarations beforehand?

推荐答案

不幸的是实体组成部分对XML的良好性规则( 2.1格式良好的XML文档)。好像你说你想要的 XDocument.Load 加载什么名义上是一个XML文件,但实际上不符合规则,它不会这样做,相当合理。

Unfortunately entities form part of the well-formedness rules for XML (2.1 Well-Formed XML Documents). It seems like you're saying you want the XDocument.Load to load what is notionally an XML file, but does not in fact conform to the rules, which it won't do, quite reasonably.

如果您的用户通过你什么应该是XML文件,但有不确定的实体,那么无论你必须让他们提供有效的格式文件,或管理在加载时的不正确youself,在已经提出的方式。

If your users are passing you what are supposed to be XML files, but that have undefined entities, then either you have to get them to provide the files in a valid format, or manage the incorrectness youself at load-time, in the ways that have been suggested.

在我看来,从你的限制,即最巧妙的办法是遵循链接到的例子,并创建一些设置来传递到的XMLReader 沿行(验证的DOM XML文档)。

It seems to me, from your restrictions, that the neatest approach would be to follow the example linked-to and create some settings to pass into the XMLReader along the lines of (Validating an XML Document in the DOM).

如果有其中没有定义,并且不公开模式上市的公司,你需要创建自己的模式定义你所需要的所有实体。因此,创建一个通用的设置的XMLReader 它引用您自己的自定义模式。添加必要的实体此架构的某些文件无法加载,然后你会建立你需要为了使XML文件是有效的来定义所有的entites的列表。

If there are entities which aren't defined and aren't listed in public schemas, you'll need to create your own schema which defines all the entities you need. So, create a generic settings for the XMLReader which references your own, custom schema. Add the necessary entities to this schema as certain files fail to load and then you'll build up a list of all the entites that you need to define in order that the XML files are valid.

然后,对于每个试图加载文档,创建一个的XMLReader 使用设置文件上方和调用的XDocument(的XMLReader)的过载。

Then, for each document you try to load, create an XMLReader for the file using the settings above and call the XDocument(XMLReader) overload.

这篇关于覆盖或使用LINQ在C#中忽略未申报的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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