Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi again,
now I'm using the XDocument class:

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 as I mentioned before that 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 alot..

Many thanks.
Posted
Updated 17-Nov-12 23:44pm
v4

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.
 
Share this answer
 
Comments
M.S.S.E 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 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.
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:

C#
    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
 
Share this answer
 
v2
Comments
Dominic Abraham 18-Nov-12 1:31am    
If the solution is helpful, please mark it as answer.
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[^]
 
Share this answer
 
Thanx for kuthuparakkal http://www.codeproject.com/Members/kuthuparakkal

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
 

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