473,787 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLReader skip current element

For example, i have some part of XML file.

<AppSettings>
<Object ClassVersion="1 .0.0.0" Type="AppSettin gs">
<Fields>
<Field Name="App_ID" Type="System.In t32">
<Value>
<int>-1</int>
</Value>
</Field>
<Field Name="AppDate Type="System.Da teTime">
<Value>
<dateTime>200 7-05-25T00:00:00</dateTime>
</Value>
</Field>
<Field Name="AppFileNa me" Type="System.St ring">
<Value>
<string>TEST 03222007.daf</string>
</Value>
</Field>
<Field Name="AppVersio n" Type="System.St ring">
<Value>
<string>1.0.3.3 </string>
</Value>
</Field>
<Field Name="_ClassVer sion" Type="System.St ring">
<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.MoveToCo ntent (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

Jun 5 '07 #1
13 5773
On Jun 5, 4:29 pm, Alex <a...@douweb.or gwrote:

<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

Jun 5 '07 #2
<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 XmlSerializatio n 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.
Jun 5 '07 #3
Alex wrote:
For example, i have some part of XML file.

<AppSettings>
<Object ClassVersion="1 .0.0.0" Type="AppSettin gs">
<Fields>
<Field Name="App_ID" Type="System.In t32">
<Value>
<int>-1</int>
</Value>
</Field>
<Field Name="AppDate Type="System.Da teTime">
As you can see, its corrupted, because AppDate doesn't gave second ".
I am getting exception when reader.MoveToCo ntent (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/
Jun 5 '07 #4
ok :(

is it possible to read in some another way, but a bit automatically,
and skip problem like that as i need ?
i mean not to use XmlReader, because it can't jump, but use smth else.
But for sure i dont want to write to xmlfile all-all fields manually
(this is just serialization of classes' fields i need).

but, if exception appears - skip field

?

Jun 5 '07 #5
On Tue, 05 Jun 2007 09:06:51 -0700, Alex <al**@mail.ruwr ote:
is it possible to read in some another way, but a bit automatically,
and skip problem like that as i need ?
i mean not to use XmlReader, because it can't jump, but use smth else.
But for sure i dont want to write to xmlfile all-all fields manually
(this is just serialization of classes' fields i need).

but, if exception appears - skip field
No. The general-purpose XML classes have no practical way to make
intelligent decisions about where to start looking again for valid data.
The only way to do what you want, even in some limited way, is to do
everything yourself.

You as a person can look at the file visually and tell where valid data
again starts, but that's because you have a LOT of "meta-information"
about the XML and can recognize things that would never appear inside
quoted text, but which are definitely part of the XML structure. If you
want your code to handle that, you will need to write it yourself, taking
advantage of this knowledge. If you do this, you will likely want to
implement your entire XML reading code from scratch, so that when you run
across something that doesn't make sense you can recover immediately based
on where you've already read.

Personally, I would not bother. As has been pointed out, the XML is
simply invalid. It's not going to be invalid unless some user hand-edits
the file and starts mucking it up, and once you assume users may do that,
it is impossible to ensure that you can in any sensible way recover from
their doings. You should definitely make sure that bad data doesn't bring
your application crashing down, but it's not reasonable for a user to
expect you to come up with some graceful way to reconstruct the invalid
data in the general case, and so you should probably not waste a lot of
time implementing code that does so.

Pete
Jun 5 '07 #6
Alex <al**@mail.ruwr ote:
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.

Sure, i made file to be invalid manually, because i want to add some
improvements to my code, to avoid or solve this problem.
Is there any real reason why you need to handle an invalid XML file?
Most XML-based applications don't, as far as I'm aware. (Obviously XML
editors have to, but other than that...)
This is just fragment, now file size is 100KB and will be bigger
later.
Also, this file is like XmlSerializatio n 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.
Why would the user have to fill anything out again? Why are you
expecting invalid XML?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 5 '07 #7
On Tue, 05 Jun 2007 10:47:27 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
Is there any real reason why you need to handle an invalid XML file?
Most XML-based applications don't, as far as I'm aware. (Obviously XML
editors have to, but other than that...)
Well, and in fact I'm not sure that XML editors have to either. As an
imprecise but similar example, consider Visual Studio's code editor. If
you miss some sort of closing quote, comment closure, closing bracket,
etc. the editor makes no attempt to recover from that. It just shows you
that there's a problem, treating the file as "valid" all the way up to the
point where it knows for sure it's not valid (which is often the end of
the file).

I can imagine someone writing an XML editor that goes to a lot of effort
to try to detect and correct invalid XML, just as the OP wants to do in
his program. But it would surprise me if this is the norm, even when
looking only at XML editors.

Pete
Jun 5 '07 #8
Peter Duniho <Np*********@nn owslpianmk.comw rote:
Is there any real reason why you need to handle an invalid XML file?
Most XML-based applications don't, as far as I'm aware. (Obviously XML
editors have to, but other than that...)

Well, and in fact I'm not sure that XML editors have to either. As an
imprecise but similar example, consider Visual Studio's code editor. If
you miss some sort of closing quote, comment closure, closing bracket,
etc. the editor makes no attempt to recover from that. It just shows you
that there's a problem, treating the file as "valid" all the way up to the
point where it knows for sure it's not valid (which is often the end of
the file).
It depends on quite how broken you make it.

If you miss off a semi-colon or have a random extra character like "+"
between statements, it's still syntactically invalid, but it recovers
quickly. An extra closing brace certainly confuses it though, yes.
I can imagine someone writing an XML editor that goes to a lot of effort
to try to detect and correct invalid XML, just as the OP wants to do in
his program. But it would surprise me if this is the norm, even when
looking only at XML editors.
Maybe it's just the ones I've used - and that's only from memory,
admittedly...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 5 '07 #9
On Tue, 05 Jun 2007 12:47:18 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
It depends on quite how broken you make it.

If you miss off a semi-colon or have a random extra character like "+"
between statements, it's still syntactically invalid, but it recovers
quickly. An extra closing brace certainly confuses it though, yes.
I suppose "recovers" is in the eye of the beholder. What I see when one
leaves off a semi-colon is that the end of the statement where the
semi-colon was expected is flagged. However, the only reason it can do
that is that it is apparent upon seeing the first thing that doesn't make
sense in that statement (ie, the next statement) where the error is.

But I don't really see that the editor has "recovered" . It is simply
pointing out the first place it has detected a problem. Just as the
compiler won't compile a file even though it could usually correctly infer
the correct location of the semicolon, it's not really like the VS editor
has judged the remainder of the file correct and accurate. In fact, it
gives up on a variety of automatic stuff once it's stumbled (for example,
I've lost count of the number of times that I don't get Intellisense
feedback because of a localized compiler-type error in my source code).

Compilers, code editors, and XML editors alike can all make inferences
about what the input data *should* look like, and try to produce correct
behavior based on those inferences. But my experience (granted, limited
in the case of XML editors, but not so limited in other areas) is that if
the input data does not comply exactly with what's expected, the user is
simply told "this data is bad...I'm not going any further until you fix
it".

Pete
Jun 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5424
by: CGuy | last post by:
Hi, I am using an XmlTextReader to read an xml file. It may happen that the file is present in the disk, but it may be empty (0 bytes). I would like to find out whether the xml file contains a valid root node or not. How do I do this? This is what I need if(File.Exists(fileName)) {
2
3718
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and public inside the class)
0
2227
by: Matthew Heironimus | last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation (http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA, and #xD#xA character combinations should be converted to a single #xA character. According to the "Reading XML with the XmlReader" section of the ".NET Framework Developer's Guide" on-line help, the XmlReader will not perform this normalization by default. You can cause the XmlReader to perform this
1
5363
by: Matthew Heironimus | last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation (http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA, and #xD#xA character combinations should be converted to a single #xA character. According to the "Reading XML with the XmlReader" section of the ".NET Framework Developer's Guide" on-line help, the XmlReader will not perform this normalization by default. You can cause the XmlReader to perform this...
1
1730
by: Logician | last post by:
Task: To Read an XML document and handle data contents in .NET for new processing Request: Any details on how to read an XML document processing all nodes and content in .NET Problems: Duplicate DATA tags, end nodes (ley01) and then data lower down, system loses its positions My Experience: Handling XML/HTML via Transforms, writing XML using
1
2314
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code: Public Class Form1 Inherits System.Windows.Forms.Form
1
2067
by: Marc Gravell | last post by:
I have an xml-reader positioned at an element. I want to read a: all of the attributes of the element, and b: all of the immediate child-elements of the current element. The first (attributes) seems very simple: if (reader.MoveToFirstAttribute()) { do { name = reader.Name;
5
4480
by: TdarTdar | last post by:
Hello, I am very new to the xmlreader in asp.net 2, I was just tring to read the contents of is in the xml output. here is the sample code: Dim settings As New XmlReaderSettings() Dim reader As XmlReader reader = XmlReader.Create(XMLOutput, settings) While reader.Read() If reader.IsStartElement() Then If reader.IsEmptyElement Then
6
2407
by: Rob Meade | last post by:
Hi all, I'm having a few difficulties with the above, ie I cant find any good examples anywhere of how to do what I want to do! I have an xml file on my desktop which I want to read in. Having checked the info for XML file reading it suggests that with .net 2.0 you should use the XMLReader class...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.