Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-22
7
Medium Priority
?
182 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:Thalox
Thalox earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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…

730 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