Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm using the XDocument class to update the value after creating the document:

C#
        XDocument doc2 = XDocument.Parse(@"
    <kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:kml='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>
        <Placemark>
             <name>Test</name>
             <LookAt>
            <longitude>11111111111</longitude>
            <latitude>1111111111111</latitude>
            <altitude>500</altitude>
            <range>500</range>
            <tilt></tilt>
            <heading>11111111</heading>
            <altitudeMode>relativeToGround</altitudeMode>
           </LookAt>
          <styleUrl>#msn_ylw-pushpin70</styleUrl>
          <Point>
            <coordinates>111111111,1111111111</coordinates>
          </Point>
      </Placemark>
</kml>");




So I need to update the "longitude" element content so I used;

C#
doc2.Element("kml").Element("longitude").Value = "XXXXX";


I get now the exception "Object reference not set to an instance of an object"
I know that there is a problem reaching the "longitude" element so what is the method which enables me to do it, because I tried many times and searched a lot..

Many thanks.
Posted
Updated 18-Nov-12 0:15am
v2

1 solution

Try this :
C#
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;

XDocument doc2 = XDocument.Parse(@"
    		<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
                <placemark>
		             <name>Test</name>
			         <lookat>
				    <longitude>11111111111</longitude>
				    <latitude>1111111111111</latitude>
				    <altitude>500</altitude>
				    <range>500</range>
				    <tilt></tilt>
				    <heading>11111111</heading>
				    <altitudemode>relativeToGround</altitudemode>
			       </lookat>
			      <styleurl>#msn_ylw-pushpin70</styleurl>
			      <point>
				    <coordinates>111111111,1111111111</coordinates>
			      </point>
              </placemark>
        </kml>");

XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable()); 
xnm.AddNamespace("x", "http://www.opengis.net/kml/2.2");

var longitude = doc2.XPathSelectElement("//x:longitude", xnm);
//var longitude = doc2.XPathSelectElement("/x:kml/x:Placemark/x:LookAt/x:longitude", xnm); this also works!
longitude.Value = "XXXXX";
 
Share this answer
 
v2
Comments
M.S.S.E 19-Nov-12 2:57am    
Thank you very much, this solved my problem.
Kuthuparakkal 19-Nov-12 3:48am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900