Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if xml file is empty or not using c#

Tags:

c#

xml

Hello everyone I want to check my xml file if it is empty or not. I am trying to update one xml data to another for this I am using the following code.Now Please tell me how can I check if my xml file has data or not Here is the code I am using for update my xml file

protected void CheckUpdates()
{
    StringReader strReader = new StringReader("..\\xml\\Updatelist.xml");
    XmlReader reader = XmlReader.Create(strReader);
    try
    {
       while (reader.Read())
       {
           var originalXmlDoc = XDocument.Load("..\\xml\\list.xml"); var newXmlDoc = XDocument.Load("..\\xml\\Updatelist.xml");

           foreach (var newElement in newXmlDoc.Element("blocker").Elements("lst"))
           {
               newElement.Value.Trim();
               if (!originalXmlDoc.Element("blocker").Elements("lst")
                       .Any(oldElement => oldElement.Value.Trim().Equals(
                       newElement.Value.Trim(),
                       StringComparison.InvariantCultureIgnoreCase)))
                {
                   originalXmlDoc.Element("blocker").Add(new XElement("lst", newElement.Value));
                }
             }
             originalXmlDoc.Save("..\\xml\\list.xml", SaveOptions.None);

             XmlDocument doc = new XmlDocument();
             doc.Load("..\\xml\\Updatelist.xml");
             doc.DocumentElement.RemoveAll();
             doc.Save("..\\xml\\Updatelist.xml");
          }
       }
    catch (XmlException ex)
    {
       //Catch xml exception
       //in your case: root element is missing
    }
}

I am Getting this error

Data at the root level is invalid. Line 1, position 1.

Please tell me how can I check if my Updatelist.xml is empty or not?

Now I get this error

like image 374
Azad Chouhan Avatar asked Jan 06 '14 05:01

Azad Chouhan


People also ask

Can XML be empty?

Empty XML ElementsAn element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.

Can mysql store XML?

The most common way to store XML in MySQL is to use the LOAD_FILE() function to open an entire XML document, store it in a variable, and insert the variable into a table column.


1 Answers

Two ways to do it.

The first is to read the file and check its structure in order to see if there are any children in it. Keep in mind that the property ChildNodes returns only the children on the specific level of the XML DOM.

XmlDocument xDoc = new XmlDocument();
if (xDoc.ChildNodes.Count == 0) { 
    // It is empty 
}else if (xDoc.ChildNodes.Count == 1) { 
    // There is only one child, probably the declaration node at the beginning
}else if (xDoc.ChildNodes.Count > 1) { 
    // There are more children on the **root level** of the DOM
}

The second way would be to catch the respective XMLException thrown when the document is loaded.

try
{
    XmlDocument doc = new XmlDocument();
    doc.Load("test.xml");
}
catch (XmlException exc)
{
    //invalid file
}

Hope I helped!

like image 76
Pantelis Natsiavas Avatar answered Sep 29 '22 23:09

Pantelis Natsiavas