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

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
Azad Chouhan
  • 277
  • 1
  • 6
  • 18
  • 1
    Please refer [here][1] [1]: http://www.codeproject.com/Questions/287593/how-to-check-if-xml-document-is-blank-or-has-value – pravprab Jan 06 '14 at 05:36

2 Answers2

11

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!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
  • I think you got this from http://stackoverflow.com/questions/1260063/determine-if-xml-file-contains-data-c-sharp right? but unfortunately this not helped him – Vignesh Kumar A Jan 06 '14 at 06: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... – Pantelis Natsiavas Jan 06 '14 at 06: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 – Azad Chouhan Jan 06 '14 at 06: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. – Pantelis Natsiavas Jan 06 '14 at 06: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

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115