0

I have a VB.Net 4.7.2 desktop application that is throwing an exception occasionally. The exception is usually, "Root element is missing".

When this happens, I am able to view the file after the fact and the root element is there.

The file is written to the share by a remote Linux system and is accessed by this application using an SMB share.

I thought there might be a timing issue with the file being written to the share, so I implemented a retry and I still get the same exception after trying 3 times, 10 seconds apart.

The xmlPath is passed in as a parameter.

' xmlPath is the UNC path to a local network share.
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(xmlPath)

When I view the XML file at the file location, here's what I see.

<reportrequest Version="1.0">
  <report ReportGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ReportCode="LENDPG_1" ABSReportCode="" Title="Lender Portal Collateral Screen Basic" Description="CS Basic Report" Price="0">
    <account AccountNumber="6551198" Address="000 Main St" City="Spokane" State="WA" ZipCode="99201" />
    <contact FirstName="Person" LastName="Ordering" Email="person@domain.com" Phone="000-000-0000" />
    <outputtypecoll>
      <outputtype Type="XML" FileName="" />
    </outputtypecoll>
    <featurecoll>
      <feature Name="CSRDISTANCE" Value="false" />
      <feature Name="CSRDBSTATUS" Value="false" />
      <feature Name="CSRELEVATION" Value="false" />
      <feature Name="FINDTP" Value="TRUE" />
      <feature Name="DETAILS" Value="TRUE" />
    </featurecoll>
    <targetpropertycoll>
      <targetproperty SiteIndex="0" PropertyGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ExternalPropertyGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" MonitoringGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" PortfolioGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" OrderGUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" PropertyName="My Property" Address="1 Wherever Ave" City="Sometown" State="CA" ZipCode="99999" Latitude="30.111111" Longitude="-110.88888">
        <georeferenced Address="My Property" City="Sometown" State="CA" ZipCode="99999" Latitude="30.111111" Longitude="-110.88888" Fips="" County="" PostalCity="" />
        <searchdistancecoll SetID="0000" SetName="LendPort Collateral" MaxDistance="5280" />
        <edrdatacoll>
          <edrdata Name="REPORTCOST" Value="0.00" />
          <edrdata Name="BORROWER" Value="" />
          <edrdata Name="LOANTYPE" Value="Other" />
          <edrdata Name="LOANNUMBER" Value="" />
          <edrdata Name="PROPERTYTYPE" Value="" />
          <edrdata Name="DESTHOST" Value="www.xxxxxxxxxxx.com" />
        </edrdatacoll>
      </targetproperty>
    </targetpropertycoll>
  </report>
</reportrequest>

This certainly has a root element.

What can be causing this seemingly bogus exception?

  • Are you sure that `Load()` takes a filename? It may expect a string containing the xml or a filestream. Hence it is parsing the filename, not the file content and the error would make perfect sense. – HEllRZA Jul 06 '22 at 19:57
  • Yeah. I checked that. It does take a string "filename" In addition, It works most of the time. – Terence Wight Jul 06 '22 at 20:18
  • If you look at a file that *does* work, is there any obvious difference to one that doesn't work? Is there an XSD file associated with the XML? – Andrew Morton Jul 07 '22 at 19:17

1 Answers1

1

Try adding this as the first line of the file

<?xml version="1.0" encoding="UTF-8"?>

EDIT:

Change this

Dim xmlDoc As New XmlDocument()
xmlDoc.Load(xmlPath)

to this

    Dim xmlDoc As New XmlDocument()
    Try
        xmlDoc.Load(xmlPath)
    Catch ex As Exception
        Dim xe As XElement
        xe = XElement.Load(xmlPath)
        Stop
    End Try
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • If that were a solution, the majority of these calls to xmlDoc.Load would not be successful. Right? – Terence Wight Jul 06 '22 at 20:39
  • @TerenceWight - I don't use XmlDocument. The XML you posted seems correct so I took a guess. – dbasnett Jul 06 '22 at 20:51
  • @TerenceWight - see edits. – dbasnett Jul 06 '22 at 21:01
  • I found peoplemhabing the same problem – HEllRZA Jul 07 '22 at 16:08
  • @HEllRZA - what? You? – dbasnett Jul 07 '22 at 17:05
  • Sorry, misclicked on my phone. maybe this helps. Sounds pretty similar. https://techhelpnotes.com/c-root-element-is-missing-5/ Also, it could be because of the network drive. Samba is not really great. Did you test it with a local file? – HEllRZA Jul 07 '22 at 20:14
  • 1
    @HEllRZA Yes. I have tried it with local and have not been able to reproduce the error. I suspect the Samba share and Gluster file system at this point. – Terence Wight Jul 20 '22 at 22:03
  • Then try catch and retry (with delay) could be a viable solution to work around the read error. Also, you could use `IO.File.ReadAllText()` to read the file (and maybe catch a different error) – HEllRZA Jul 21 '22 at 05:05