Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
Hi again,
now I'm using the XDocument class:
 
                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 as I mentioned before that 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 alot..
 
Many thanks.
Posted 17-Nov-12 0:40am
M.S.S.E400
Edited 18-Nov-12 0:44am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
 
Try it yourself, you may find it is not as difficult as you think!
 
There are a lot of examples out there, that just need you to tweek them to your specific needs. Asking for "I need the full code starting from loading the file and finishing with saving and closing it." is not trying to do it and failing, it's purely trying to get us to do it for you because you can't be bothered, and that helps nobody in the long term.
  Permalink  
Comments
M.S.S.E at 17-Nov-12 6:11am
   
Sorry for inconvenience but I explained that I faced some problems since I've been trying with some errors and that ends up posting here...
 
maybe you could have giving me a hand rather than your reply, and the question is question whoever asked!.
 
I hope if any one could help me please. becauseI'm beginner in XML .
Thanks.
OriginalGriff at 17-Nov-12 6:26am
   
There is a difference between being a beginner, and being a beginner who wants it all done for him.
The latter is not going to learn anything, the former will probably learn when he tries to do it himself.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
 
You can find many good articles in internet describing how to read/traverse/update xml.
You can use LINQ to XML mechanism.
 
http://msdn.microsoft.com/en-us/library/bb387098.aspx
 
LINQ to XML[^]
 
Or you can simply
1. Read the xml file in to dataset
2. Modify the dataset
3. Update back in to xml.
For Ex:
 
                DataSet oDs = new DataSet();
            oDs.ReadXml("D:\\yourfile.xml");
            if (oDs != null && oDs.Tables.Count > 0 && oDs.Tables[0].Rows.Count > 0  && oDs.Tables[1].Rows.Count > 0)
            {
                for (int i = 0; i < oDs.Tables[0].Rows.Count; i++)
                {
                    oDs.Tables[0].Rows[i]["name"] = "newName";
                    oDs.Tables[0].Rows[i]["PlaceMark_ID"] = "newID";
                }
                for (int i = 0; i < oDs.Tables[1].Rows.Count; i++)
                {
                    oDs.Tables[0].Rows[i]["lnngtitude"] = "newval";                    
                }
            }
            oDs.AcceptChanges();
            oDs.WriteXml("D:\\yourfile.xml");
 
This may not be the best solution. Try to understand various mechanisms. And have a good idea about this. You just try what you know. And if you found any errors, just post here other than expecting the full source code. All the best
  Permalink  
v2
Comments
Dominic Abraham at 18-Nov-12 1:31am
   
If the solution is helpful, please mark it as answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I completely agree with OriginalGriff. I would say it would have been better if you post the problem you faced.
 
You can take a look at the following link, it provides one of the many ways to update an xml document.
 
LINQ to XML Update Element Values[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanx for kuthuparakkal http://www.codeproject.com/Members/kuthuparakkal
 
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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 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