0
<ENVELOPE>
    <HEADER>
        <TALLYREQUEST>Import Data</TALLYREQUEST>
    </HEADER>
    <BODY>
        <IMPORTDATA>
            <REQUESTDESC><REPORTNAME>All Masters</REPORTNAME><STATICVARIABLES><SVCURRENTCOMPANY>MSIT</SVCURRENTCOMPANY></STATICVARIABLES></REQUESTDESC>
            <REQUESTDATA>
                <TALLYMESSAGE>
    <Entry_x0020_NO.>125</Entry_x0020_NO.>
    <DATE>12</DATE>
    <GUID>1258</GUID>
    <NARRATION>1542</NARRATION>
    <VOTURETYPENAME>456</VOTURETYPENAME>
    <NAME>achuth</NAME>
    <AMOUNT>250</AMOUNT>
  </TALLYMESSAGE>
            </REQUESTDATA>
        </IMPORTDATA>
    </BODY>
</ENVELOPE>

the above XML is taken as input and convert to excel as below

Entry No.   date  guid   narration   voucher   name   amount  
125         12     1258     1542      456      achuth  250

the code for me throws error null reference as belowenter image description here

Achuth hadnoor
  • 312
  • 3
  • 14
  • take a look at this answer http://stackoverflow.com/questions/2494967/how-to-convert-xml-to-excel-file-programmatically – Mostafiz Apr 10 '16 at 10:54

1 Answers1

1

You can just cast XElement instance to string instead of accessing its Value property to avoid such exception in the case where the target element is not found within a parent element :

......
new YourClass()
{
    YourClassProperty = (string)s.Element("Your_element_that_may_not_exists"),
    ......
}
......
har07
  • 83,990
  • 12
  • 70
  • 116
  • i dint get u sorry – Achuth hadnoor Apr 10 '16 at 11:09
  • @achuthhadnoor It is likely that one of your `s.Element()` part returns `null`, and trying to access `Value` property of a `null` should trigger the [null reference exception](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). So instead of doing `s.Element(...).Value`, try using the the above suggested pattern `(string)s.Element(...)` – har07 Apr 10 '16 at 11:24