Solved

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

Posted on 2004-04-22
7
177 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
ID: 10893950
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
ID: 10894849
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
ID: 10896342
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Assisted Solution

by:Thalox
Thalox earned 250 total points
ID: 10898055

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
ID: 10898242
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
ID: 10899098
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
ID: 10899165
I split the points because I used code from both mmarinov and Thalox to get things working. Thanks for the help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Entity Framework 7 41
Code works but it's slow 28 70
EF5: Question about Metadata Artifact Processing 4 15
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

14 Experts available now in Live!

Get 1:1 Help Now