[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I write to this xml file?

Posted on 2009-05-03
5
Medium Priority
?
235 Views
Last Modified: 2012-05-06
How do I write a new animal to this xml file?

Best would be if I could use something like this:
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("Animals.xml"));
public void WriteXmlFile(String text)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("Animals.xml"));
 
 
 
        }
 
-----------------------------------------------------------------
xml file
 
<?xml version="1.0" encoding="utf-8" ?>
<Animals>
  <Animal>
    <Type>Apa</Type>
  </Animal>
  <Animal>
    <Type>Gris</Type>
  </Animal>
  <Animal>
    <Type>Ko</Type>
  </Animal>
  <Animal>
    <Type>Katt</Type>
  </Animal>
  <Animal>
    <Type>Hund</Type>
  </Animal>
</Animals>

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:cauos
ID: 24289032

public void WriteXmlFile(String text)
        {
        
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Animals.xml"));
 
        XmlNode root = doc.DocumentElement;
 
        //Create a new node.
        XmlElement elem = doc.CreateElement("Animal");
        elem.InnerXml = "<Type>Monkey</Type>";
 
        //Add the node to the document.
        root.AppendChild(elem);        
        doc.Save("Animals.xml");
}

Open in new window

0
 

Author Comment

by:Mickeys
ID: 24289047
Almost. :-)

I think it opens the correct file....in my project which is here
C:\Documents and Settings\MFL\Dokumenter\Visual Studio 2005\WebSites\WebSite\WebSite\Tenta1

and saves it here
C:\Programmer\Microsoft Visual Studio 8\Common7\IDE\Animals.xml

How do I mange to save it in my project?
0
 
LVL 4

Expert Comment

by:cauos
ID: 24289126
you can specify the full path where you want to save file to
doc.Save(@"C:\Documents and Settings\mdarabseh\My Documents\Visual Studio 2005\WebSites\WebSiteCSharp\Animals.xml");
0
 

Author Comment

by:Mickeys
ID: 24289151
ok that works. Just one more question.
How come it finds the file in the correct space when opening but when saving I need to write the hole path?
0
 
LVL 4

Accepted Solution

by:
cauos earned 2000 total points
ID: 24289234
because when you read the file you use Server.MapPath("Animals.xml")
server.mappath give you server path within your website; but in the example i provided i did't add server.mappath method; you can do this if you want and its recommended specially if you want to publish your website on different machine or different folder in the hard drive.
 
 doc.Save(Server.MapPath("Animals.xml"));
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question