XmlException: Root element is missing (but it's not)

318 Views Asked by At

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="[email protected]" 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?

1

There are 1 best solutions below

8
dbasnett On

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