Unable to load the xml because of empty value

457 views Asked by At

I am trying load the below XML values

<?xml version="1.0" standalone="yes"?>
<Data>
   <curmsg>
       <event> myevent </event>
       <clientid> 123456 </clientid>
       <appid> 1123456 </appid>
       <timestamp> December 13 2016 00:00:00 </timestamp>
       <clientname>  TEST ACCOUNT </clientname>   
       <accountnum> 123 </accountnum>   
       </backlink>    
       <key>AAAAC</key>
       <fileid>1234</fileid>   
       <filename>LKTEST1.1</filename>      
       <filesize>30</filesize>      
       <eekey>LKTEST</eekey>      
       <pjobid>DFSAQE3Z4</pjobid>    
       <filecategory>Proceed</filecategory>
   </curmsg>
</Data>

C# code:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmldata);

The above LoadXml function was throwing an exception because the <backlink> tag has only ending tag. It was working when I provided the starting tag but my requirement is I want to load the XML when tag has no starting tag (empty values).

2

There are 2 answers

0
Alexei Levenkov On BEST ANSWER

This is invalid XML and no built-in XML tools will be able to read it.

Your options:

  • make whoever produces that file to create valid XML
  • use string.Replace to replace broken nodes before loading it as XML (hacky, will likely break later when authors of the text file will introduce more invalid XML there).
  • use more forgiving parsers - i.e. HtmlAgilityPack is explicitly designed to handle broken HTML and may be able to produce partial tree out of your text file.
0
nvoigt On

You cannot.

Why? Because </backlink> is not an empty tag. It's simply a syntax error. <backlink/> is an empty tag.

You probably want to use a correct XML file.