Click here to Skip to main content
12,622,652 members (28,673 online)
Rate this:
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='' xmlns:gx='' xmlns:kml='' xmlns:atom=''>

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
Updated 18-Nov-12 0:44am
Rate this: bad
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.
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 .
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


You can find many good articles in internet describing how to read/traverse/update xml.
You can use LINQ to XML mechanism.

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();
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";

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
Dominic Abraham 18-Nov-12 1:31am
If the solution is helpful, please mark it as answer.
Rate this: bad
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[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Thanx for kuthuparakkal

using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
XDocument doc2 = XDocument.Parse(@"
    		<kml xmlns="" xmlns:gx="" xmlns:kml="" xmlns:atom="">

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
Web01 | 2.8.161128.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
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