Solved

xml

Posted on 2011-09-02
12
482 Views
Last Modified: 2012-05-12
Hey guys, i can not get the nodes text from my xml document?

here is the document innerxml

<marker id="1" r_name="Rit Restaurant" address="232 Canehaven drive" area="ROSSLYN" location="ERASMUS VILLAGE" p_code="0250" Latitude="-25.6236510" Longitude="28.0887790" />

here is my c# code :

 XmlDocument Document = new XmlDocument();
        Document = XmlData(Latitude, Longitude);

        if (Document != null)
        {
            XmlNodeList nodeList = Document.SelectNodes("markers");

            foreach (XmlNode node in nodeList)
            {
                string address = node.SelectSingleNode("address").InnerText;
            }
        }


please help?

i cant get the address?
0
Comment
Question by:JCWEBHOST
12 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36472446
>>  Document.SelectNodes("markers");
>>  <marker id="1"...

I suspect the SelectNodes isn't correct, or is some XML missing ?
0
 

Author Comment

by:JCWEBHOST
ID: 36472465
here my code:

    public void searchLocationsNear(GeoCode latlng)
    {
        double Latitude = latlng.Placemark.coordinates.lat;
        double Longitude = latlng.Placemark.coordinates.lng;

        XmlDocument Document = new XmlDocument();
        Document = XmlData(Latitude, Longitude);

        if (Document != null)
        {
            XmlNodeList nodeList = Document.SelectNodes("markers");

            foreach (XmlNode node in nodeList)
            {
                string address = node.SelectSingleNode("address").InnerText;
            }
        }
    }

    public XmlDocument XmlData(double dmlSearchLatitude, double dmlSearchLongitude)
    {
        m.DmlSearchLatitude = dmlSearchLatitude;
        m.DmlSearchLongitude = dmlSearchLongitude;
        m.Area = ddArea.SelectedItem.Text;

        XmlReader rdrXMLLocations = null;
        rdrXMLLocations = m.proc_Location_List();

        Response.Expires = 0;
        Response.ContentType = "text/xml";
        XmlDocument oDocument = new XmlDocument();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        using (rdrXMLLocations)
        {
            while (!rdrXMLLocations.EOF)
            {
                rdrXMLLocations.MoveToContent();
                sb.Append(rdrXMLLocations.ReadOuterXml());
            }
            rdrXMLLocations.Close();
        }

        oDocument.LoadXml(sb.ToString());

        return oDocument;
    }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36472482
OK, and why did you post that code ??


repeat:
>>  Document.SelectNodes("markers");
>>  <marker id="1"...

I suspect the SelectNodes isn't correct, or is some XML missing ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36472484
Or put another way the code you posted in the question look rather like:

Hi, I want to find objects of type X with the following code, by the way this is how some other object of type A is defined...
0
 

Author Comment

by:JCWEBHOST
ID: 36472491
here my xml

(Document).InnerXml

vaule
<markers><marker id="1" r_name="Rit Restaurant" address="232 Canehaven drive" area="ROSSLYN" location="ERASMUS VILLAGE" p_code="0250" Latitude="-25.6236510" Longitude="28.0887790" /></markers>
0
 

Author Comment

by:JCWEBHOST
ID: 36472517
thats my full xml document
0
 

Author Comment

by:JCWEBHOST
ID: 36472555
here is my javascript which i am trying to convert to c# code:

 
var markers = xml.documentElement.getElementsByTagName('marker');

            if (markers.length == 0) 
            {
                sidebar.innerHTML = 'No results found.  Please try widening your search area.';
                map.setCenter(new GLatLng(startingLat, startingLng), startingZoom);
                return;
            }

            var bounds = new GLatLngBounds();
            for (var i = 0; i < markers.length; i++) 
            {
                var address = markers[i].getAttribute('address');
                var area = markers[i].getAttribute('area');
                var location = markers[i].getAttribute('location');
                var p_code = markers[i].getAttribute('p_code');
                var point = new GLatLng(parseFloat(markers[i].getAttribute('Latitude')), parseFloat(markers[i].getAttribute('Longitude')));

                var marker = createMarker(point, address, area, location, p_code);

                map.addOverlay(marker);
                var sidebarEntry = createSidebarEntry(marker, address, area, location);
                sidebar.appendChild(sidebarEntry);
                bounds.extend(point);
            }

Open in new window

0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 166 total points
ID: 36472560
First of all.


The foreach loop is looping the Markers
so until there it looks ok.

then you are searching in your Marker for the Node "address" ??
there are no child Nodes only attributes.
so you need to take the attribute value (something like node.Attributes("address"))

poor beggar
0
 

Author Comment

by:JCWEBHOST
ID: 36472578

 node.Attributes("address"))


Not working getting null ref to object
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 167 total points
ID: 36472768
OK, some XML was missing - That bit looks like it should work.

Are you actually getting any nodes returned at:
XmlNodeList nodeList = Document.SelectNodes("markers");
0
 
LVL 1

Assisted Solution

by:Vitor Silva
Vitor Silva earned 167 total points
ID: 36477543
Shouldn't you be doing XmlNodeList nodeList = Document.SelectNodes("markers/marker");, because you want to be set on the nodes "marker" inside the node "markers" and that's where you want loop.
and then loop trough the nodes to get the sttribute address using node.Attributes("address").
0
 

Author Closing Comment

by:JCWEBHOST
ID: 36482766
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question