Click here to Skip to main content
13,701,966 members
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 26 May 2003

XPath reader-writer

, 26 May 2003
Rate this:
Please Sign up or sign in to vote.
Writes data in an XML file using XPath

Sample Image - XPathStore.jpg


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

For more information about XPath see

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.

XPathStore storage = new XPathStore();
XmlNode node = storege.QueryCreatNode
MyEditBox.Text = node.Value;

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

XPathStore storage = new XPathStore();
XmlNode node = storege.QueryCreatNode
node.Value = "Data for attribute Z (date, list of primary keys, etc...)";

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.

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.

//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.


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


About the Author

Richard van den Berg
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralXpath Pin
antoine@orchus-tech23-Oct-03 9:31
memberantoine@orchus-tech23-Oct-03 9:31 
GeneralAnd the idea works, you know! Pin
D.D. de Kerf28-May-03 0:28
memberD.D. de Kerf28-May-03 0:28 
GeneralRe: And the idea works, you know! Pin
Daniel Cazzulino3-Jun-03 2:37
memberDaniel Cazzulino3-Jun-03 2:37 
GeneralNice! Pin
Rocky Moore27-May-03 20:45
memberRocky Moore27-May-03 20:45 
QuestionWhat is an XPath Pin
peterchen26-May-03 23:12
memberpeterchen26-May-03 23:12 
AnswerRe: What is an XPath Pin
Paul Selormey26-May-03 23:34
memberPaul Selormey26-May-03 23:34 
GeneralRe: What is an XPath Pin
peterchen27-May-03 0:09
memberpeterchen27-May-03 0:09 
GeneralRe: What is an XPath Pin
Notorious SMC28-May-03 15:34
memberNotorious SMC28-May-03 15:34 
GeneralRe: What is an XPath Pin
FingerStyle29-May-03 11:38
memberFingerStyle29-May-03 11:38 
AnswerRe: What is an XPath Pin
Richard van den Berg27-May-03 0:11
memberRichard van den Berg27-May-03 0:11 
GeneralRe: What is an XPath Pin
nehalkumar10-May-06 1:05
membernehalkumar10-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180906.1 | Last Updated 27 May 2003
Article Copyright 2003 by Richard van den Berg
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid