locked
Problem on Reading Large XML File RRS feed

  • Question

  • Hi,

    I am trying to develop wpf application which will read a large and complex XML file from same computer. when i debugging my program it will show reading data from xml file but data row is not stored in List object and terminate the program show the error is "Object reference not set to an instance of an object "

    here is my code:

            public static List<CustomerClass> LoadDisbursement()
             { 
                List<CustomerClass> ListCustomerRecords=new List<CustomerClass>();
                var custs = from c in XElement.Load(@"E:\DisbursementNo_30.xml").Elements("Table1") select c;
                foreach (var customer in custs)
                {
                    CustomerClass lCustomer = new CustomerClass
                    {
                        AgreementID = customer.Element("AgreementID").Value,
                        POAgreementID = customer.Element("POAgreementID").Value,
        .......

        ...........

                        IsSecondSystem = customer.Element("IsSecondSystem").Value,
                        NoOfWomanBeneficiary = customer.Element("NoOfWomanBeneficiary").Value
                    };
                    //ListCustomerRecords.Add(lCustomer);
                    ListCustomerRecords.Add(lCustomer);
                }
                return ListCustomerRecords;
            } 

    and Main code is:

         private void btnLoad_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    List<CustomerClass> CustomerList = new List<CustomerClass>();
                    CustomerList = DAL_DisbursementNo_30.LoadDisbursement();
                    listView1.DataContext = CustomerList;
                }
                catch (Exception ex)
                {
                    lblMessage.Content = ex.Message;
                }
            }

    And My xml file is:

    <?xml version="1.0" standalone="yes"?>
    <NewDataSet>
      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="Table">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="HouseHoldNo" type="xs:decimal" minOccurs="0" />
                    <xs:element name="POHouseHoldNo" type="xs:decimal" minOccurs="0" />
    ...................
                    <xs:element name="UnionID" type="xs:decimal" minOccurs="0" />
                    <xs:element name="OccupationID" type="xs:decimal" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Table1">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="AgreementID" type="xs:decimal" minOccurs="0" />
                    <xs:element name="POAgreementID" type="xs:decimal" minOccurs="0" />
    ......................
                    <xs:element name="IsSecondSystem" type="xs:string" minOccurs="0" />
                    <xs:element name="NoOfWomanBeneficiary" type="xs:int" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>

      <Table>
        <HouseHoldNo>38630</HouseHoldNo>
        <POHouseHoldNo>1394</POHouseHoldNo>
          .........................
        <UnionID>1151</UnionID>
        <OccupationID>8</OccupationID>
      </Table>
      <Table>
        <HouseHoldNo>38631</HouseHoldNo>
        <POHouseHoldNo>1395</POHouseHoldNo>
        ......................
        <UnionID>1153</UnionID>
        <OccupationID>8</OccupationID>
      </Table>

      <Table1>
        <AgreementID>37847</AgreementID>
    ........................
        <NoOfWomanBeneficiary>3</NoOfWomanBeneficiary>
      </Table1>
      <Table1>
        <AgreementID>37848</AgreementID>
    ..........................
        <NoOfWomanBeneficiary>2</NoOfWomanBeneficiary>
      </Table1>

    </NewDataSet>


                  
    Monday, March 25, 2013 11:09 AM

Answers

All replies

  • On what line do you get the error?

    LS


    Lloyd Sheen

    Monday, March 25, 2013 11:16 AM
  • In  public static List<CustomerClass> LoadDisbursement() method "ListCustomerRecords.Add(lCustomer);" this line is not execute. 
    Monday, March 25, 2013 11:41 AM
  • This makes no sense.  Can you tell me which of the two objects referenced in the line of code is Null?  It will be either ListCustomerRecords or ICustomer but like I said this makes no sense given what you are showing us.


    Lloyd Sheen

    I have to think that the line of code that is giving you the error is not the add line.

    • Edited by sqlguy Monday, March 25, 2013 12:23 PM
    Monday, March 25, 2013 12:22 PM
  • If the xml file is really big then I think you might do better using xmlreader.

    http://msdn.microsoft.com/en-us/library/bb387035.aspx

    And an example based on this

    http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

    You might also want to consider reading through each entry in a for loop to explore the data you get, temporarily at least.

    • Proposed as answer by Lisa Zhu Friday, March 29, 2013 12:38 PM
    • Marked as answer by Lisa Zhu Wednesday, April 3, 2013 2:45 PM
    Monday, March 25, 2013 1:39 PM
  • Hi green_energy,

    I am writing to  check the status of the issue on your side. What about this problem now?
    Would you mind letting us know the result of the suggestions?

    Regards,



    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 12:38 PM
  • Hi Lisa Zhu,

    My problem is solved now, There is no problem on my code. I was facing this error because of my xml file. I have an complex xml file it's sequence of column attribute doesn't meet with the object attribute.

    • Proposed as answer by Lisa Zhu Sunday, March 31, 2013 8:01 AM
    • Marked as answer by Lisa Zhu Wednesday, April 3, 2013 2:45 PM
    Saturday, March 30, 2013 4:15 AM
  • Hi green_energy,

    I am glad to know you found the problem and thanks for telling us the result. Have  a nice day!

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, March 31, 2013 8:03 AM