Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Article

XPath reader-writer

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
26 May 20033 min read 115.9K   3.2K   45   11
Writes data in an XML file using XPath

Sample Image - XPathStore.jpg

Introduction

With this sample, you can read and write data using Xpath.

For more information about XPath see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/xpath_starter_47qr.asp.

This code first tries to query the XML document with the given XPath, and if it has no match, the XPath is created for you, and the requested node is returned to you. This is done by parsing the XPath and creating the required nodes and attributes. The next time the same XPath query is fired, it will return the node.

What you can do with the code

I have written this code for a project two years ago, in C++. It was used to store large amounts of client settings locally, on the client computer. That program displayed about 30 forms, each form having about three list controls with database records and two date time controls. Here the user selects data to generate a report. For each user/page/control combination, the last selection was stored in the XML file. The next time the user returns to that form, it is in the same state.

I have recently converted it to C#, because I want to store the list control selections from an ASP.NET app on the server. The program has a limited number of users, so the XML file is kept small. Also the user gets the same settings if he uses another computer.

Using the code

Copy the file XPathStore.cs to your project. Add reference to assembly system.xml to your project. Add the code below.

C#
XPathStore storage = new XPathStore();
XmlNode node = storege.QueryCreatNode
                ("settings/NodeX[@attribY=\"valueA\"]/@attribZ");
MyEditBox.Text = node.Value;

The code for writing and changing the XML data is almost the same.

C#
XPathStore storage = new XPathStore();
XmlNode node = storege.QueryCreatNode
                ("settings/NodeX[@attribY=\"valueA\"]/@attribZ");
node.Value = "Data for attribute Z (date, list of primary keys, etc...)";
storage.SaveSettings();

Points of interest

You can use this test application to see if an XPath query is valid (for this code). If the XPath is formatted wrong, the main XPath query throws an exception. The result is that in the data edit box, an error text string is displayed. On every text change in the XPath edit box, the document is queried with that XPath and the results are displayed in the data edit box.

C#
XPathStore storage = new XPathStore();
XmlNode node = storage.QueryCreatNode(XPath.Text);
data.Text = (node==null ? "\r\n#### XPATH RETURNED NULL ####":node.Value);

When you design your XPath query, first make a sample and if it works you can construct it runtime.

C#
//sXPath = "settings/MyApp/Account[@ID="109"]/FormData/Form[@ID="
//                Form3"]/Filter[@Name="lbTest"]/@data";
sXPath = "settings/MyApp/Account[@ID=\"" + sUniquID + 
            "\"]/FormData/Form[@ID=\"" +
            sUniquFormID + "\"]/Filter[@Name=\"" + 
            sUniqueControlID + "\"]/@data";

When you use this code in an ASP.NET app, remember to give the aspnet account (used by Internet Information Server) write rights, to write the XML file.

In a normal XML document, an XPath query can return more than one match. In this sample only one hit may occur. If you design your XPath queries with that in mind, it shouldn't be a problem.

I have used four types of data storage in my C# project.

  1. In the cookie I stored the last used login account (local client computer settings)
  2. In the session state I stored a few data retrieved from the database at login.
  3. The XML file generated and maintained by this code, stores user dependent data that needs no transactions or protection from loss or damage.
  4. The program data orders, suppliers etc. is stored in a database.

In the XPathStore there are a lot of Console.WriteLine() methods added. These are added to show you how the XPath is handled and when nodes are queried or created. Remove these lines later.

If you delete the XML file, the only thing lost is the data. The file is recreated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralXpath Pin
antoine@orchus-tech23-Oct-03 9:31
antoine@orchus-tech23-Oct-03 9:31 
GeneralAnd the idea works, you know! Pin
D.D. de Kerf28-May-03 0:28
D.D. de Kerf28-May-03 0:28 
GeneralRe: And the idea works, you know! Pin
Daniel Cazzulino [XML MVP]3-Jun-03 2:37
Daniel Cazzulino [XML MVP]3-Jun-03 2:37 
Then, maybe you two could join your code and make it even better Wink | ;)

Daniel Cazzulino
www.deverest.com.ar
dotnetopensrc.sf.net

Coauthor of:
Beg. C# Web Apps
ASP.NET Components Toolkit
Beg. Web Programming w/VB.NET & VS.NET
Pro ASP.NET Server Controls w/C#
GeneralNice! Pin
Rocky Moore27-May-03 20:45
Rocky Moore27-May-03 20:45 
QuestionWhat is an XPath Pin
peterchen26-May-03 23:12
peterchen26-May-03 23:12 
AnswerRe: What is an XPath Pin
Paul Selormey26-May-03 23:34
Paul Selormey26-May-03 23:34 
GeneralRe: What is an XPath Pin
peterchen27-May-03 0:09
peterchen27-May-03 0:09 
GeneralRe: What is an XPath Pin
Nick Seng28-May-03 15:34
Nick Seng28-May-03 15:34 
GeneralRe: What is an XPath Pin
kaaos29-May-03 11:38
kaaos29-May-03 11:38 
AnswerRe: What is an XPath Pin
Richard van den Berg27-May-03 0:11
Richard van den Berg27-May-03 0:11 
GeneralRe: What is an XPath Pin
nehalkumar10-May-06 1:05
nehalkumar10-May-06 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.