title: Checking if root element exist in Xml using qt c++
body: <blockquote>
    <p><strong>I have an Xml file:</strong></p>
  </blockquote>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt;
   &lt;AppConfig&gt;
   &lt;DefaultCulture&gt;&lt;/DefaultCulture&gt;
   &lt;StartupMessageTitle&gt;&lt;/StartupMessageTitle&gt;
   &lt;StartupMessageText&gt;&lt;/StartupMessageText&gt;
   &lt;StartupMessageIcon&gt;Information&lt;/StartupMessageIcon&gt;
   &lt;DefaultProfileSettings&gt;
   &lt;DriverName&gt;wia&lt;/DriverName&gt;
   &lt;UseNativeUI&gt;false&lt;/UseNativeUI&gt;
   &lt;IconID&gt;0&lt;/IconID&gt;
   &lt;MaxQuality&gt;false&lt;/MaxQuality&gt;
   &lt;AfterScanScale&gt;OneToOne&lt;/AfterScanScale&gt;
   &lt;Brightness&gt;0&lt;/Brightness&gt;
   &lt;Contrast&gt;0&lt;/Contrast&gt;
   &lt;BitDepth&gt;C24Bit&lt;/BitDepth&gt;
   &lt;PageAlign&gt;Left&lt;/PageAlign&gt;
   &lt;PageSize&gt;Letter&lt;/PageSize&gt;
   &lt;Resolution&gt;Dpi100&lt;/Resolution&gt;
   &lt;PaperSource&gt;Glass&lt;/PaperSource&gt;
   &lt;/DefaultProfileSettings&gt;
   &lt;!--
   &lt;AutoSaveSettings&gt;
   &lt;FilePath&gt;&lt;/FilePath&gt;
   &lt;ClearImagesAfterSaving&gt;false&lt;/ClearImagesAfterSaving&gt;
   &lt;Separator&gt;FilePerPage&lt;/Separator&gt;
   &lt;/AutoSaveSettings&gt;
    --&gt;
   &lt;/AppConfig&gt;
  </code></pre>
  
  <blockquote>
    <p>I need to check if root elements "AutoSaveSettings" exists in xml using qt c++ . if the root elements exists then remove the commented line before  and after the Auto save settings? How can we do it in qt c++. How do I perform this operation in c++.Check if exists start element or root element</p>
  </blockquote>
  
  <pre><code>  #include &lt;QtCore&gt;
    #include &lt;QtXml/QDomDocument&gt;
    #include &lt;QDebug&gt;
    #include &lt;QFile&gt;
    #include &lt;QXmlStreamReader&gt;
  
    bool elementExists(const QFile &amp;file, const QString &amp;elementName)      
    {
  
       QXmlStreamReader reader(&amp;file);
  
       while (reader.readNextStartElement())
        {
          if(reader.name() == elementName)
           {
             return true;
           }
       }
       return false;
     }
  
    int main(int argc,char *argv[])
     {
       QCoreApplication a(argc,argv);
  
       QDomDocument document;
       QFile file = "C:/Program Files (x86)/NAPS2/appsettings.xml";
       if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
       {
           qDebug()&lt;&lt;"Failed to open the file";
           return -1;
       }
       else{
           if(!document.setContent(&amp;file))
           {
               qDebug()&lt;&lt; "Failed to load Document";
               return -1;
           }
           file.close();
       }
  
       elementExists(file,"AutoSaveSettings");
  
       qDebug()&lt;&lt; "Finished";
  
       return a.exec();
      }
  </code></pre>