Xml has invalid charecters

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I would like to know how to close xml tags if an error occurs.

I use a try catch block in a method to write a xml file, the method tries to write data to xml , if an error occurs a error text file is written with the error but the remaining tags are not written..and leaves unclosed tags , is there a way to close the remaining tags?

Thank You

InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
thank you for your reply.

I know the error is not from the writing of the xml...let me try to explain what I am doing.

I am scraping a website of cars for sale...there are about 20,000 listings of classic cars for sale on this website...my code goes to each page and gets a set of attributes for each car listing.
for example:

each listing has
title
sale price
dealer
year
make
model

there are over 35 different attributes for each car....everything works until the code reaches a description with invalid charecters in it, I do not know what the invalid charecters are. I look at the listing where the error occurs and I cannot see a invalid character in the description text.....when the code breaks on this error it writes the error to a error txt file I create...then it continues to the next listing, but it does not close the xml tags , so when it tries to write the next listing it throws another error....and so on and so on....is there a way if or when it gets an invalid character error to close the xml tags?

thank you
InkedGFX
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
The error is in the writing of the xml. If it was not, you would not have exceptions :) The cause might however be somewhere else. As asked, show the code. In the catch or finally, you probably have to add the closing tags.

With regards to a possible cause of the error. If you use something like below, you might get the exception when the text that you try to write contains non-ascii characters.
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Encoding = Encoding.ASCII;
                //settings.Encoding = Encoding.Unicode;

                using (XmlWriter xmlWriter = XmlWriter.Create(strFileName, settings))
                {
                    ...
                    ...
                }



I had the issue with the coding; the line in above snippet that is commented out solved it. In my case, the 'using' was embedded in a try/catch and the xml file was empty (or corrupted, can't remember).

So for some useful help, post the relevant part of your code.
 
Top Bottom