0

I have an xml file with a namespace and i can read it properly.It has an outer node,called 'Items' which has multiple child nodes,50 of them.(So 50 child nodes called 'ReceiverPoints').When i check the console,its size is correct i.e 50 but when i check the print out,all the output is a repetition of just the first ReceiverPoint node.

I would like to save each receiver point into the database.According to all the examples i have seen,my implementation seems fine.But it gives me wrong results.Could someone help me see what i'm missing?This is the xml file

public List<ReceiverPoint> ReadFile()
    {
        receiverList = new List<ReceiverPoint> ();

        Console.WriteLine ("Now in read file method :" + fileLocation);

            xmldoc = new XmlDocument ();
            xmldoc.Load (fileLocation);

        XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager (xmldoc.NameTable);

        //nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
        nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");



        XmlElement rootElement = xmldoc.DocumentElement;

        XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);

        ReceiverPoint receiverPoint = new ReceiverPoint ();
foreach(XmlNode childNode in nodeList)
        {
            receiverPoint.CloseDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CloseDate", nameSpaceManager).InnerText;
            receiverPoint.CreateDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateDate", nameSpaceManager).InnerText;  
            receiverPoint.CreateWho = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateWho", nameSpaceManager).InnerText;
            receiverPoint.Easting = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Easting", nameSpaceManager).InnerText;
            receiverPoint.Elevation = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Elevation", nameSpaceManager).InnerText;
            receiverPoint.IsDeployed = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsDeployed", nameSpaceManager).InnerText; 
            receiverPoint.IsManual = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsManual", nameSpaceManager).InnerText;
            receiverPoint.LastModifyDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LastModifyDate", nameSpaceManager).InnerText;
            receiverPoint.Latitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LatitudeWGS84", nameSpaceManager).InnerText;
            receiverPoint.Line = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Line", nameSpaceManager).InnerText;
            receiverPoint.Longitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LongitudeWGS84", nameSpaceManager).InnerText;
            receiverPoint.ReceiverType = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:ReceiverType", nameSpaceManager).InnerText;
            receiverPoint.Station = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Station", nameSpaceManager).InnerText;




            //Get all the values stored in the receiver point object
            string station = receiverPoint.Station;
            string line = receiverPoint.Line;
            string elevation = receiverPoint.Elevation;
            string latitude = receiverPoint.Latitude;
            string longitude = receiverPoint.Longitude;
            string isDeployed = receiverPoint.IsDeployed;
            string easting = receiverPoint.Easting;
            string receiverType = receiverPoint.ReceiverType;
            string closeDate = receiverPoint.CloseDate;
            string createDate = receiverPoint.CreateDate;
            string createWho = receiverPoint.CreateWho;
            string lastModifyDate = receiverPoint.LastModifyDate;


            Console.WriteLine ("String lat : " + latitude);

            Console.WriteLine ("String lon : " + longitude);

            Console.WriteLine ("String create date : " + createDate);

            Console.WriteLine ("String create who : " + createWho);

            //Save the data to the db
            saveDataToDatabase (station,line,elevation,latitude,longitude,isDeployed,easting,receiverType,closeDate,createDate,createWho,lastModifyDate);


        }

        receiverList.Add (receiverPoint);
return receiverList;
    }

2 Answers 2

1

This code work for me:

public List<ReceiverPoint> ReadFile()
        {
            var receiverList = new List<ReceiverPoint>();

            Console.WriteLine("Now in read file method :" + "");

            var xmldoc = new XmlDocument();
            xmldoc.Load(@"D:\users\..\Downloads\ReceiverPoints.xml");

            XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(xmldoc.NameTable);

            //nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
            nameSpaceManager.AddNamespace("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");



            XmlElement rootElement = xmldoc.DocumentElement;

            XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);


            foreach (XmlNode childNode in nodeList)
            {
                ReceiverPoint receiverPoint = new ReceiverPoint();
                receiverPoint.CloseDate = childNode.SelectSingleNode("ns:CloseDate", nameSpaceManager).InnerText;
                receiverPoint.CreateDate = childNode.SelectSingleNode("ns:CreateDate", nameSpaceManager).InnerText;
                receiverPoint.CreateWho = childNode.SelectSingleNode("ns:CreateWho", nameSpaceManager).InnerText;
                receiverPoint.Easting = childNode.SelectSingleNode("ns:Easting", nameSpaceManager).InnerText;
                receiverPoint.Elevation = childNode.SelectSingleNode("ns:Elevation", nameSpaceManager).InnerText;
                receiverPoint.IsDeployed = childNode.SelectSingleNode("ns:IsDeployed", nameSpaceManager).InnerText;
                receiverPoint.IsManual = childNode.SelectSingleNode("ns:IsManual", nameSpaceManager).InnerText;
                receiverPoint.LastModifyDate = childNode.SelectSingleNode("ns:LastModifyDate", nameSpaceManager).InnerText;
                receiverPoint.Latitude = childNode.SelectSingleNode("ns:LatitudeWGS84", nameSpaceManager).InnerText;
                receiverPoint.Line = childNode.SelectSingleNode("ns:Line", nameSpaceManager).InnerText;
                receiverPoint.Longitude = childNode.SelectSingleNode("ns:LongitudeWGS84", nameSpaceManager).InnerText;
                receiverPoint.ReceiverType = childNode.SelectSingleNode("ns:ReceiverType", nameSpaceManager).InnerText;
                receiverPoint.Station = childNode.SelectSingleNode("ns:Station", nameSpaceManager).InnerText;




                //Get all the values stored in the receiver point object
                string station = receiverPoint.Station;
                string line = receiverPoint.Line;
                string elevation = receiverPoint.Elevation;
                string latitude = receiverPoint.Latitude;
                string longitude = receiverPoint.Longitude;
                string isDeployed = receiverPoint.IsDeployed;
                string easting = receiverPoint.Easting;
                string receiverType = receiverPoint.ReceiverType;
                string closeDate = receiverPoint.CloseDate;
                string createDate = receiverPoint.CreateDate;
                string createWho = receiverPoint.CreateWho;
                string lastModifyDate = receiverPoint.LastModifyDate;


                Console.WriteLine("String lat : " + latitude);

                Console.WriteLine("String lon : " + longitude);

                Console.WriteLine("String create date : " + createDate);

                Console.WriteLine("String create who : " + createWho);

                //Save the data to the db
                //saveDataToDatabase(station, line, elevation, latitude, longitude, isDeployed, easting, receiverType, closeDate, createDate, createWho, lastModifyDate);

                receiverList.Add(receiverPoint);
            }


            return receiverList;
        }
Sign up to request clarification or add additional context in comments.

4 Comments

even with those changes,it still shows data from only the first receiver point 50 times.
Oh, yes, i can see your second mistake : when you use the selectSingleNode on the childNode, you are already in "/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint" so just put the relative path (e.g ns:CloseDate for the first)
I followed your advice and made the edit for the relative path of the childnodes.But the result is still the same.Getting only the first childnode
Yes, that definitely worked!Thank you so much.I can see in the console that all the nodes have been read.On the list view,i get only one row read from the sqlite db,but that's a different problem.Your solution worked.Thanks for your help :)
0

Move receiverList.Add(receiverPoint) inside the foreach loop.

1 Comment

I moved that line inside the foreach loop but i'm getting the same error.

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.