Solved

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

Posted on 2004-04-22
7
178 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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