Solved

Need to read from and write to an XML file from web forms

Posted on 2004-04-22
7
175 Views
Last Modified: 2010-04-07
I have a new application I am working on for my company.  There are 5 seperate aspx files with various information being entered.  The user selects which page they want to view from a menu.  Each page has its own "Save" button.   No DB is involved with this, and I am told that one will not be needed for this application.   I am pretty new to asp.net, so I need some guidance.

When a user chooses the "Save" button on a page, the information should be written to an XML file.  If they then go to another page and save it, that information should be saved to the same XML file without overwriting any of the info saved from the previous aspx page.  How would I go about doing this?

The next issue involves how I would go about updating info that already exists in the XML file.  Say a user goes to a page and changes some information and then saves it.  There is already info saved in and element that will not need to be updated.  Is there an easy way to go about doing this?  Essentially, I would need to check to see if the element that stores this info already exists.  If not, then it needs to be added to the file, and if it does then I need to update the info.

I think I have figured out how to read the XML file so that I can display the info in the proper places on the aspx page.  AT this point, I am dumping the XML file into a dataset and then using that to display the data I need for a particular page.  Once again, is there an easy way to see if something exists within a data set?
0
Comment
Question by:mattgoolsby
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
you can use this

XmlDocument doc = new XmlDocument ();
doc.Load( "the _XML_path_file");
XmlNode root = doc.DocumentElement;

then you can do this for adding new xmlnodе
XmlElement elem = doc.CreateElement("name");
elem.InnerText = "ExpertsExchange";
root.AppendChild (elem)
doc.Save("the _XML_path_file");

B..M
0
 

Author Comment

by:mattgoolsby
Comment Utility
Some of that would work, but I need some more info.

What if a child node already exists?  How can I go about searching for it and replacing the info with the new info?

Here is how I am thinking the XML doc would look if all 5 pages have been saved.  Let me know if I am wrong in my thinking.

<page1>
   <p1field1>info</p1field1>
   <p1field2>info</p1field2>
   ...
</page1>
<page2>
   <p2field1>info</p2field1>
   <p2field2>info</p2field2>
   ...
</page2>
...

Assume I will do things such as add a "p1field3" if it does not exists, or update the info in "p1field1" if it is changed.  Also assume that "page2" might not exist and need to be added at some point,.
0
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
you will have to create you own methods to search for you if there is a childnode already added or not

public bool IsChildAdded ( string NodeName, XmlNode root )
{
  foreach ( XmlNode Child in root.ChildNodes )
   {
     if ( Child.Name == NodeName )
       return true;
     if ( Child.HasChildNodes )
       IsChildAdded ( NodeName, Child );
    }

return false;
}

HTH
B..M
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Assisted Solution

by:Thalox
Thalox earned 250 total points
Comment Utility

you can also do the following:

(asuming you want to update / insert a value for p1field3)

XmlDocument doc = new XmlDocument();
// load Xml

XmlNode node = doc.DocumentElement.SelectSingleNode("//page1/p1field3");

if (node == null) {
  // add node
  doc.DocumentElement.SelectSingleNode("//page1").AppendChild(doc.CreateElement("p1field3"));
  doc.DocumentElement.SelectSingleNode("//page1/p1field3").Value = "newValue";
} else {
  // change node
  node.Value = "newValue";
}

// save XML

NOTE: if you want to insert a node for a new page (a page that doesnt exist) you first have to create the node for the page (you cannot use the syntax from selectsinglenode)
0
 

Author Comment

by:mattgoolsby
Comment Utility
I am using the following code to create a new node when one does not exist.  However, when I open the XML file, the new node is not in there.  Am I doing something wrong?

           Dim xmlDOC As New XmlDocument
           Dim rootNode As XmlNode

            xmlDOC.Load(Server.MapPath("MattTest.xml"))

            rootNode = xmlDOC.SelectSingleNode("/Configurations")

            If rootNode Is Nothing Then
                xmlDOC.CreateNode(XmlNodeType.Element, "Configurations", "")
                xmlDOC.Save(Server.MapPath("MattTest.xml"))
            Else
            End If
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
Comment Utility
you have to modify your code like this

Dim xmlDOC As New XmlDocument
           Dim rootNode As XmlNode

            xmlDOC.Load(Server.MapPath("MattTest.xml"))

            rootNode = xmlDOC.SelectSingleNode("/Configurations")

            If rootNode Is Nothing Then
                Dim newNode as XmlNode = xmlDOC.CreateNode(XmlNodeType.Element, "Configurations", "")
                rootNode.AppendChild( newNode )
                xmlDOC.Save(Server.MapPath("MattTest.xml"))
            Else
            End If


B..M
0
 

Author Comment

by:mattgoolsby
Comment Utility
I split the points because I used code from both mmarinov and Thalox to get things working. Thanks for the help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now