Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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.

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.

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.

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.

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

Share

About the Author

Richard van den Berg
Web Developer
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralXpath Pinmemberantoine@orchus-tech23-Oct-03 9:31 
GeneralAnd the idea works, you know! PinmemberD.D. de Kerf28-May-03 0:28 
GeneralRe: And the idea works, you know! PinmemberDaniel Cazzulino3-Jun-03 2:37 
GeneralNice! PinmemberRocky Moore27-May-03 20:45 
QuestionWhat is an XPath Pinmemberpeterchen26-May-03 23:12 
AnswerRe: What is an XPath PinmemberPaul Selormey26-May-03 23:34 
GeneralRe: What is an XPath Pinmemberpeterchen27-May-03 0:09 
GeneralRe: What is an XPath PinmemberNotorious SMC28-May-03 15:34 
GeneralRe: What is an XPath PinsussFingerStyle29-May-03 11:38 
AnswerRe: What is an XPath PinmemberRichard van den Berg27-May-03 0:11 
GeneralRe: What is an XPath Pinmembernehalkumar10-May-06 1:05 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 27 May 2003
Article Copyright 2003 by Richard van den Berg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid