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

1

1 Answer 1

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"),
    ......
}
......
Sign up to request clarification or add additional context in comments.

2 Comments

i dint get u sorry
@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. So instead of doing s.Element(...).Value, try using the the above suggested pattern (string)s.Element(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.