Writing/Overwriting to specific XML file from ASP.NET code behind

1k Views Asked by At

I have a XML world map which basically takes two inputs (its a XML file with corresponding CSS files and stuff): the country input and country address. So when I manually enter the data into the XML file (Country name and country address) the country on the map changes its color and on hovering over that country I can see what I've entered.

I have a list of all countries in my DB. So I was thinking of there is any way for me to write in all those countries from my DB into the XML file. I was thinking of something like this:

for(int i=0; i<list.count;i++)
{
    list[i].CounryName = //write it into the XML file;
    list[i].CountryUserAddress = //Write it into the XML file;
}

So the idea is to when the for loop goes on and on, every country is written in into the XML file. I don't have any significant experience working with XML files in ASP.NET and I'm stuck on dry land here. All this should be done via code behind. Can someone help me out with this, or at least point me into the right direction?

Thanks heaps!

P.S. I've forgot to mention that I should be actually overwriting the already existing XML file, not creating a new one...

1

There are 1 best solutions below

1
On BEST ANSWER

Here's an example of how you could do this with the data you provided:

    public string EditDoc()
    {
        string filename = "Path/MyFileName.xml";
        List<string> list = new List<string>();

        if (File.Exists(filename)) //we have the file, so update it
        {
            XmlDocument myDoc = new XmlDocument(); //create a document object
            myDoc.Load(filename); //load existing info
            XmlNode root = myDoc.DocumentElement; //the root node ("Country")
            XmlNode nodeToUpdate = root.SelectSingleNode("CountryName"); //select the node to update

            nodeToUpdate.Value = "new info"; //give it a new value

            myDoc.Save(filename); //save the document
        } 
        else //didn't find the file
        {
            XmlDocument myDoc = new XmlDocument(); //create a new document

            XmlNode countryList = myDoc.CreateElement("CountryList");
            myDoc.AppendChild(countryList);

            for (int i = 0; i < list.Count; i++)
            {
                XmlNode country = myDoc.CreateElement("Country"); //create the parent "Country" element
                myDoc.AppendChild(countryList); //append to the list

                XmlNode countryName = myDoc.CreateElement("CountryName"); //create element for "CountryName"
                countryName.AppendChild(myDoc.CreateTextNode(list[i].CountryName)); //add data from list index
                country.AppendChild(countryName); //append this as a child to "Country"

                XmlNode countryUserAddress = myDoc.CreateElement("CountryUserAddress"); //create element for "CountryUserAddress"
                countryUserAddress.AppendChild(myDoc.CreateTextNode(list[i].CountryUserAddress)); //add data from list index
                country.AppendChild(countryUserAddress); //append as a child to "Country"
            }

            myDoc.Save(filename); //save the document
        }
    }

The general idea is to traverse the document's tree and select that value to update. There may be a better way to do this, but this is the way I'm familiar with. Similarly, you can create an xml document in the same manner.

The subject matter is different, but this helped me tremendously in understanding reading/writing XML data: this link

EDIT: I updated the code slightly to make the parent element "CountryList" and append each "Country" in the DB to that. The document will end up coming out something like:

<CountryList>
    <Country>
        <CountryName>Blah</CountryName>
        <CountryUserAddress>Blah</CountryUserAddress>
    </Country>
    <Country>
        <CountryName>Blah</CountryName>
        <CountryUserAddress>Blah</CountryUserAddress>
    </Country>
</CountryList>