Click here to Skip to main content
13,044,935 members (45,591 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi all,
I'm using the XDocument class to update the value after creating the document:

                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;

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 18-Nov-12 0:13am
M.S.S.E401
Updated 18-Nov-12 0:15am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this :
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";
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100