5,534,119 members and growing! (17,186 online)
Email Password   helpLost your password?
Languages » XML » XML/XSLT     Intermediate

XPath reader-writer

By Richard van den Berg

Writes data in an XML file using XPath
C#, .NET, Win2K, WinXP, Windows, Visual Studio, ASP.NET, Dev

Posted: 26 May 2003
Updated: 26 May 2003
Views: 58,867
Bookmarked: 36 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 5.27 Rating: 4.59 out of 5
0 votes, 0.0%
1
1 vote, 7.1%
2
0 votes, 0.0%
3
2 votes, 14.3%
4
11 votes, 78.6%
5

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

About the Author

Richard van den Berg



Occupation: Web Developer
Location: Netherlands Netherlands

Other popular XML articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralXpathmemberantoine@orchus-tech10:31 23 Oct '03  
GeneralAnd the idea works, you know!memberD.D. de Kerf1:28 28 May '03  
GeneralRe: And the idea works, you know!memberDaniel Cazzulino3:37 3 Jun '03  
GeneralNice!memberRocky Moore21:45 27 May '03  
GeneralWhat is an XPathsupporterpeterchen0:12 27 May '03  
GeneralRe: What is an XPathsupporterPaul Selormey0:34 27 May '03  
GeneralRe: What is an XPathsupporterpeterchen1:09 27 May '03  
GeneralRe: What is an XPathmemberNotorious SMC16:34 28 May '03  
GeneralRe: What is an XPathsussFingerStyle12:38 29 May '03  
GeneralRe: What is an XPathmemberRichard van den Berg1:11 27 May '03  
GeneralRe: What is an XPathmembernehalkumar2:05 10 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2003
Editor: Smitha Vijayan
Copyright 2003 by Richard van den Berg
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project