Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Handling empty XML File

Posted on 2011-10-12
6
Medium Priority
?
315 Views
Last Modified: 2012-05-12
Dear all

I need to validate if a specific XML file is empty before importing the file into the system
How can I do this .?

That is I need to check = xmlFile.isEmpty then I will perform an Operation

Thanks in Advance
0
Comment
Question by:ZURINET
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 36960438
What do you mean with an empty xml-file ? Without any roottag or without data-bocks ?

http://stackoverflow.com/questions/1260063/determine-if-xml-file-contains-data-c
0
 

Author Comment

by:ZURINET
ID: 36960483
Empty means Empty.. Nothing inside.. :-)

no roottags
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36960583
So the size of the file would be zero bytes?  (ie.  All you need to do is check the file size)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
KBerger earned 1000 total points
ID: 36960918
Hi ZURINET,

try checking the file-length like this:

            string filename = AppDomain.CurrentDomain.BaseDirectory + "links.xml";

            bool isFileEmpty = false;
            if (System.IO.File.Exists(filename))
            {
                System.IO.FileInfo info = new System.IO.FileInfo(filename);
                if (info.Length == 0)
                {
                    isFileEmpty = true;
                }
            }

Cheers,

-Kristof
0
 
LVL 7

Assisted Solution

by:Rahul_Gade
Rahul_Gade earned 1000 total points
ID: 36960983

In case you are planning to use XmlDocument:

            try
            {
                XmlDocument doc = new XmlDocument();
                FileStream fStream = new FileStream("C:\\test.xml", FileMode.Open);
                if (fStream.Length > 0)
                {
                    doc.Load(fStream);
                }
                else
                {
                    Debug.Print("File is empty");
                }
            }
            catch (FileNotFoundException ex)
            {
                Debug.Print("File not found");
            }
        }

-Rahul
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 36961046
i will just try to load the file to XML document object and if an error comes means the file is not valid or the data is not there
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question