Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
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.E400
Edited 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 at 19-Nov-12 2:57am
   
Thank you very much, this solved my problem.
Kuthuparakkal at 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
0 CPallini 480
1 Sergey Alexandrovich Kryukov 335
2 OriginalGriff 285
3 George Jonsson 246
4 Gihan Liyanage 235
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
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