XMLReader跳过当前元素 [英] XMLReader skip current element

查看:70
本文介绍了XMLReader跳过当前元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一些XML文件。


< AppSettings>

< Object ClassVersion =" 1.0.0.0" Type =" AppSettings">

< Fields>

< Field Name =" App_ID" Type =" System.Int32">

< Value>

< int> -1< / int>

< / Value>

< / Field>

< Field Name =" AppDate Type =" System.DateTime">

< Value>

< dateTime> 2007-05-25T00:00:00< / dateTime>

< / Value>

< / Field>

< Field Name =" AppFileName" Type =" System.String">

< Value>

< string> TEST 03222007.daf< / string>

< / Value>

< / Field>

< Field Name =" AppVersion" Type =" System.String">

< Value>

< string> 1.0.3.3< / string>

< / Value>

< / Field>

< Field Name =" _ClassVersion" Type =" System.String">

< Value>

< string> 1.0.0.0< / string>

< / Value>

< / Field>

< / Fields>

< / Object>

< / AppSettings>


如您所见,它已损坏,因为AppDate没有给出第二个。

我得到例外当reader.MoveToContent(在我读取App_ID之后)

这一切都在try..catch部分...

之后我收到smth就像字符串fieldname == AppDate

Type =" ;;

我无法理解,我怎么能跳到AppFileName并跳过损坏的

AppDate?

所以,如何在catch部分我可以跳转到下一个元素? (在

应用程序的工作期间,我不知道下一个元素的名称是什么)


谢谢

解决方案

6月5日下午4:29,Alex< a ... @ douweb.org写道:


< snip>


如你所见,它已损坏,因为AppDate没有给出第二个。



对。这是一个无效的XML文件。我强烈建议你

完全拒绝这样的文件 - 试图处理像

这样的破碎文件这是一个真正的痛苦,我不知道是否XmlReader(或者任何其他.NET XML类型的支持。


Jon


<剪断>


>


如你所见,它已损坏,因为AppDate没有给出第二个。



对。这是一个无效的XML文件。我强烈建议你

完全拒绝这样的文件 - 试图处理像

这样的破碎文件这是一个真正的痛苦,我不知道是否XmlReader(或者任何其他.NET XML类型支持它。


Jon



当然,我手动将文件设置为无效,因为我想为我的代码添加一些

改进,以避免或解决这个问题。


这是只是片段,现在文件大小是100KB并且会更大

以后。

此外,这个文件就像我想要的某些类的XmlSerialization

序列化。

所以,存储的数据很大,我真的不希望用户再次填写所有内容。


所以,如果有一些解决方案,我会很高兴在这里。


Alex写道:


例如,我有一些XML文件。


< A ppSettings>

< Object ClassVersion =" 1.0.0.0" Type =" AppSettings">

< Fields>

< Field Name =" App_ID" Type =" System.Int32">

< Value>

< int> -1< / int>

< /值>

< / Field>

< Field Name =" AppDate Type =" System.DateTime">


如您所见,它已损坏,因为AppDate没有给出第二个。

我得到了reader.MoveToContent(在我读取App_ID之后)的例外情况

这一切都在try..catch部分...

之后我收到smth就像字符串fieldname = =" AppDate

Type =" ;;

我无法理解,我怎么能跳到AppFileName并跳过损坏的

AppDate ?

所以,如何在catch部分我可以跳转到下一个元素? (在

应用程序的工作期间,我不知道下一个元素的名称是什么)



XML有严格的规则,样本标记格式不正确,因此XML解析器不会解析它而是抛出异常。

无法简单地跳过格式不正确的标记。因此,您将无法使用XmlReader成功解析该标记。您必须修复

无论应用程序生成标记以生成格式良好的XML。

使用.NET使用XmlWriter可以提供帮助。

-


Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


For example, i have some part of XML file.

<AppSettings>
<Object ClassVersion="1.0.0.0" Type="AppSettings">
<Fields>
<Field Name="App_ID" Type="System.Int32">
<Value>
<int>-1</int>
</Value>
</Field>
<Field Name="AppDate Type="System.DateTime">
<Value>
<dateTime>2007-05-25T00:00:00</dateTime>
</Value>
</Field>
<Field Name="AppFileName" Type="System.String">
<Value>
<string>TEST 03222007.daf</string>
</Value>
</Field>
<Field Name="AppVersion" Type="System.String">
<Value>
<string>1.0.3.3</string>
</Value>
</Field>
<Field Name="_ClassVersion" Type="System.String">
<Value>
<string>1.0.0.0</string>
</Value>
</Field>
</Fields>
</Object>
</AppSettings>

As you can see, its corrupted, because AppDate doesn''t gave second ".
I am getting exception when reader.MoveToContent (after i read App_ID)
this all are in try..catch section...
and after that i am receiving smth like string fieldname == "AppDate
Type=";
I can''t understand, how i can jump to AppFileName and skip corrupted
AppDate ?
so, how in catch section i can jump to next element ? (during
application''s work, i dont know what is the name of next element)

Thanks

解决方案

On Jun 5, 4:29 pm, Alex <a...@douweb.orgwrote:

<snip>

As you can see, its corrupted, because AppDate doesn''t gave second ".

Right. It''s an invalid XML file. I would strongly recommend that you
completely reject such files - trying to cope with broken files like
this is a real pain, and I don''t know whether XmlReader (or any of the
other .NET XML types) support it.

Jon


<snip>

>

As you can see, its corrupted, because AppDate doesn''t gave second ".


Right. It''s an invalid XML file. I would strongly recommend that you
completely reject such files - trying to cope with broken files like
this is a real pain, and I don''t know whether XmlReader (or any of the
other .NET XML types) support it.

Jon

Sure, i made file to be invalid manually, because i want to add some
improvements to my code, to avoid or solve this problem.

This is just fragment, now file size is 100KB and will be bigger
later.
Also, this file is like XmlSerialization of some classes i want to be
serialized.
So, the data which stored are big, and i really don''t want user to
fill out all again.

So, if there is some solution about this, i will be glad to here.


Alex wrote:

For example, i have some part of XML file.

<AppSettings>
<Object ClassVersion="1.0.0.0" Type="AppSettings">
<Fields>
<Field Name="App_ID" Type="System.Int32">
<Value>
<int>-1</int>
</Value>
</Field>
<Field Name="AppDate Type="System.DateTime">

As you can see, its corrupted, because AppDate doesn''t gave second ".
I am getting exception when reader.MoveToContent (after i read App_ID)
this all are in try..catch section...
and after that i am receiving smth like string fieldname == "AppDate
Type=";
I can''t understand, how i can jump to AppFileName and skip corrupted
AppDate ?
so, how in catch section i can jump to next element ? (during
application''s work, i dont know what is the name of next element)

XML has strict rules, the sample markup is not well-formed and therefore
the XML parser will not parse it but throw an exception. There is no way
to simply skip markup that is not well-formed. So you will not be able
to parse that markup successfully with XmlReader. You have to fix
whatever application generates the markup to produce well-formed XML.
With .NET using XmlWriter can help.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


这篇关于XMLReader跳过当前元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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