Solved

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

Posted on 2004-04-22
7
179 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
Industry Leaders: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 69
Why use this lambda? 12 98
asp.net repeater server posting 5 39
history of pages 7 55
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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