473,770 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

skipping invalid nodes with XmlTextReader

I want to do something like:

bool bContinueReadin g=true;
XmlTextReader Reader=new XmlTextReader(" file.xml");
while(bContinue Reading)
{
try
{
Reader.Read();
...
}
catch
{
Reader.Skip();
}
}

To skip over any xml formatting problems (invalid characters) in the file.
However, the Skip() function does not seem to move the cursor forward and an
infinite loop results when an error occurs.
Nov 12 '05 #1
3 2842
The code should read:

bool bContinueReadin g=true;
XmlTextReader Reader=new XmlTextReader(" file.xml");
while(bContinue Reading)
{
try
{
bContinueReadin g=Reader.Read() ;
...
}
catch
{
Reader.Skip();
}
}
Nov 12 '05 #2
Stephen S Kelley wrote:
To skip over any xml formatting problems (invalid characters) in the file.
However, the Skip() function does not seem to move the cursor forward and an
infinite loop results when an error occurs.


You can't by design. XML spec requires draconian error handling - any
malformed XML document must not be parsed successfully.

--
Oleg Tkachenko [XML MVP, MCAD]
http://blog.tkachenko.com
Nov 12 '05 #3
And the XmlReader implements this W3C XML Spec. The problem with error
recovery in general is it becomes ambiguous which is generally not good for
mission critical business applications. So you will have to employ a more
forgiving parser (see SgmlReader on www.gotdotnet.com for an example).

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
Stephen S Kelley wrote:
To skip over any xml formatting problems (invalid characters) in the
file. However, the Skip() function does not seem to move the cursor
forward and an infinite loop results when an error occurs.


You can't by design. XML spec requires draconian error handling - any
malformed XML document must not be parsed successfully.

--
Oleg Tkachenko [XML MVP, MCAD]
http://blog.tkachenko.com

Nov 12 '05 #4

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

Similar topics

5
9219
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef RelatedID="118"/> <PersonName> <NameTitle Value="Mr"/> <FirstName>Clint</FirstName> <OtherName/> <AlsoKnownAs>Mr C Eastwood</AlsoKnownAs>
2
10701
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
1
16440
by: Emsi | last post by:
Hello, how can I read values of child nodes with the XmlTextReader? File format: <root> <items> <item> <field1>value1</field1> <field2>value2</field2>
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
36828
by: Brent Jenny | last post by:
I have an existing xml file taht looks like this. <?xml version="1.0" encoding="utf-8" ?> <Aircraft> <WCOJOItemType type="AircraftType" > <WCJOItemCode>03</WCJOItemCode> <WCJOItemDescription>B737-700</WCJOItemDescription> </WCOJOItemType> </Aircraft> I want to be able to add nodes to the doc using C#. I have tried the
1
1672
by: acool | last post by:
If I have an XML file in a string variable what is the best way to go about deleting specific Nodes or elements from this file??
1
1898
by: Christian H | last post by:
Hello! I am currently playing around with XmlTextReader and XmlTextWriter. I've got the TextWriter to write out nodes in this form: <node> <childnode>moredata</childnode> <childnode>moredata</childnode>
6
2727
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any further results then the XML document contains a URL to call and get the next set of 10 results. So if there were 25 results total I would get the first 10 in the first XML document, then after making a second request I would get results 11 to 20...
6
3218
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, can someone please let me know how I can read xml elements using object oriented program. I created a class to use the get and set properties however I dont know how I can pass the values from xml file to the class and use the values in my form. Thanks -- Nejadian
0
9617
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
10254
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...
0
10099
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
10036
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
9904
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...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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

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.