5

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

1

3 Answers 3

12

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!

4
  • I think you got this from stackoverflow.com/questions/1260063/… right? but unfortunately this not helped him Commented Jan 6, 2014 at 6:11
  • Actually I have used it quite recently. I don't remember how I found it. Sorry for not helping. If you want you can tell me some details and perhaps we find a solution... Commented Jan 6, 2014 at 6:16
  • @PantelisNatsiavas see i just want to check my xml is empty or not on the base of this check I want to do my further process Commented Jan 6, 2014 at 6:32
  • Sorry but I don't understand your comment. The code I posted, checks if an XML file is empty. You could use it to see if your XML file is empty. Right? I thought that the other process is not part of your question. Commented Jan 6, 2014 at 6:37
1

You can try to load the XML into XML document and catch the exception. Here is the sample code:

var doc = new XmlDocument();
try {
  doc.LoadXml(content);
} catch (XmlException e) {
  // put code here that should be executed when the XML is not valid.
}

Hope it helps.

(Or)

If you want the function (for stylistic reasons, not for performance), implement it yourself:

public class MyXmlDocument: XmlDocument
{
  bool TryParseXml(string xml){
    try{
      ParseXml(xml);
      return true;
    }catch(XmlException e){
      return false;
    }
 }

Use this function to know whether it is valid or not

Source

0

Additionally, XmlNode has a property that can be checked:

public virtual bool HasChildNodes { get; }

https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.haschildnodes?view=net-8.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.