473,799 Members | 2,842 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
13 5776
Peter Duniho <Np*********@nn owslpianmk.comw rote:
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.
It recovers to the extent that it's able to find errors later on, and
you can still use Intellisense etc.

For example, take this code:

using System;

public class Test
{
static void Main()
{
int x = 5
int y = 10;

Console.WriteLi ne("Hello");
}
}

If you type another "Console." underneath the current call to
Console.WriteLi ne, VS (2005 at least) offers Intellisense.

It's hard for me to judge exactly how well VS does as opposed to
resharper, but if you change Console.WriteLi ne to Console.Foo, I
certainly get some feedback that Foo isn't a valid member of Console.
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).
You should try Eclipse some time - it will compile (in some cases, at
least) syntactically invalid code, generating code which throws an
exception when it's got to somewhere that the compilation broke. Not
terribly handy, but quite cute.
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".
Certainly things are more limited after an error, but there's often
still *some* functionality available. If I find the time I might see
what a few XML editors do past an error - whether they still
automatically close tags, find further errors etc. Certainly the VS
2005 XML editor was able to automatically close the "blech" tag in the
below XML, despite the previous error:

<?xml version="1.0" encoding="utf-8" ?>
<foo>
<bar>
<baz text="Hello otherText="Ther e"/>

<blech></blech>
</bar>
</foo>

Also if you change </blechto </blech2it notices that as a second
error.
--
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 #11
On Tue, 05 Jun 2007 13:40:01 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
[...]
You should try Eclipse some time - it will compile (in some cases, at
least) syntactically invalid code, generating code which throws an
exception when it's got to somewhere that the compilation broke. Not
terribly handy, but quite cute.
Well, sure. I can appreciate "cute". :) But as you say, not terribly
handy. Likewise, just how handy would it be to just skip over an invalid
section of XML, when you have no idea what the overall effect of doing so
would be? Just because the remaining XML can be parsed, that doesn't mean
that it can be *used* without the part that was erroneous.
[...] Certainly the VS
2005 XML editor was able to automatically close the "blech" tag in the
below XML, despite the previous error:
I certainly agree that it *can* be done. I just am not convinced it makes
sense to bother writing the code to do so. It does seem to me that in an
editor, where the user is actively modifying the data, it makes more sense
to put the effort in, but even there I wouldn't necessarily insist on it
(even in VS there are limits to what it can recover from, and frankly it
only handles the simplest situations). I expect it's something you see in
editors that are intended to be feature-laden, considered "heavy-duty"
(that's certainly how I'd describe VS).

In a situation where the data is static though, I don't see the use in
recovering. You never know when the data that was in error was critical
to the use of the larger XML document. Just because you can successfully
parse the rest of the document doesn't mean you should, just as just
because a compiler could make an assumption about where to insert a
missing semi-colon doesn't mean it should.

Pete
Jun 5 '07 #12
Peter Duniho <Np*********@nn owslpianmk.comw rote:
[...]
You should try Eclipse some time - it will compile (in some cases, at
least) syntactically invalid code, generating code which throws an
exception when it's got to somewhere that the compilation broke. Not
terribly handy, but quite cute.

Well, sure. I can appreciate "cute". :) But as you say, not terribly
handy. Likewise, just how handy would it be to just skip over an invalid
section of XML, when you have no idea what the overall effect of doing so
would be? Just because the remaining XML can be parsed, that doesn't mean
that it can be *used* without the part that was erroneous.
On the other hand, if I open an invalid XML file it's nice to know
whether there's just one error or whether the whole thing is pooched.
[...] Certainly the VS
2005 XML editor was able to automatically close the "blech" tag in the
below XML, despite the previous error:

I certainly agree that it *can* be done. I just am not convinced it makes
sense to bother writing the code to do so. It does seem to me that in an
editor, where the user is actively modifying the data, it makes more sense
to put the effort in, but even there I wouldn't necessarily insist on it
(even in VS there are limits to what it can recover from, and frankly it
only handles the simplest situations). I expect it's something you see in
editors that are intended to be feature-laden, considered "heavy-duty"
(that's certainly how I'd describe VS).
Agreed in the last bit - and I'm *certainly* not suggesting that the OP
should try to recover.
In a situation where the data is static though, I don't see the use in
recovering. You never know when the data that was in error was critical
to the use of the larger XML document. Just because you can successfully
parse the rest of the document doesn't mean you should, just as just
because a compiler could make an assumption about where to insert a
missing semi-colon doesn't mean it should.
Oh absolutely. I was only talking about editors, where it can be handy
to be able to show more than the first error.

Even with static document reading, it *may* be useful to bomb out with
an error which has a good stab at working out where all the error parts
are, rather than just the first one. That's not the same as really
trying to recover though.

--
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 #13
On Tue, 05 Jun 2007 15:00:04 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
On the other hand, if I open an invalid XML file it's nice to know
whether there's just one error or whether the whole thing is pooched.
Sure, I agree. If you're using an editor, that would be a nice feature to
have. But that still doesn't mean it would be a ubiquitous feature in all
XML editors (though I can see how it might appear in advanced editors).
[...]
Even with static document reading, it *may* be useful to bomb out with
an error which has a good stab at working out where all the error parts
are, rather than just the first one. That's not the same as really
trying to recover though.
Nope. :)

If I wanted to provide feedback as to a place to look for the error, I
would inform the user where the last place in the file I had valid data.
That's not really the same as trying to do anything fancy with figuring
out the erroneous part though. All it requires is keep track of how far
into the file you got before you failed to generate new valid data.

It's the parsing bad data that I think is normally going to be outside the
scope of typical software. Sorry if I seem to have taken this thread off
on a tangent. I just got set off by the statement that an XML editor
*has* to handle errors. An XML editor *could* in fact just display the
text beyond the error and tell the user "I'm not going to help you with
this until you fix it". :)

Pete
Jun 6 '07 #14

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
2228
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
5364
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
1731
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
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10238
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
10030
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
7570
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
5467
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...
1
4145
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
3761
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.