Click here to Skip to main content
Licence 
First Posted 28 Apr 2004
Views 32,924
Bookmarked 8 times

How to search & modify within XML documents using XPath queries

By | 3 May 2004 | Article
XML data manipulation.

Introduction

This article is intended to show you how XPath queries have made programmers’ life easier regarding XML data manipulation. XML applications have many varieties; from simple client-side and server-side applications to web services over the Internet. However, one particular fact never changes; Data change dynamically and constantly. In this simple and short newcomer's project we will search for data within an XML document using two different XPath queries. Also, we will add a method to replace current information with new information. Since this is my first article regarding Microsoft C# and XML there might be some points which, need modification or improvement so please contact me to share your ideas or any suggestions you may have. Personally, I like to write every project from scratch! in this case, read the following steps:

Create a new project:

1 - Launch Visual Studio.Net IDE and select “Windows Application” under Visual C# Projects. Of course, you can select "Console Application" only it requires you to modify part of the source code.

2 - The controls, which, we would need are TreeView , Edit-Box, List-Box, Button & Label controls.

3 - First, let's consider the Nampespaces, which are necessary to include:

3 -1 ) System.Xml

3-2 ) System.Xml.Xpath

3-3 ) System.Text

3-4 ) System.Collections.Specialized //Was used for holding Node paths and Node attributes but I removed them to make the code smaller

Note: Please ignore System.Collections.Specialized.

4 - Define a sturcutre as below:

struct TXN 
{
   public TreeNode nnode;
   public XmlNode xnode;
   public TXN(TreeNode nnode, XmlNode xnode)
  {
    this.nnode = nnode;
    this.xnode = xnode;
   }
}

5 - Also, I did name the controls as:

private System.Winodws.Forms.GroupBox treeGroupBox;
private System.Winodws.Forms.TreeView treeXmlBody;
private System.Winodws.Forms.Button btnOpenXml;
private System.Winodws.Forms.Button btnReplace;
private System.Winodws.Forms.Button btnClose;
private System.Winodws.Forms.Label lblItem; //The item we would looking for
private System.Winodws.Forms.Label lblPath; 
private System.Winodws.Forms.ListBox listBoxPaths; 
private System.Winodws.Forms.TextBox txtItem; 
private System.Winodws.Forms.Button btnSearch;
private System.Winodws.Forms.TextBox txtReplace; //The text which should 
                                                 //replace the previous text
Some public variables too:
public string sXPathQuery; //Holds XPath query
public string sPath; //Keeps track of working area
also:
private XmlDocument xdoc; //XML object

- For more information please refer to comments or contact me. Once again, thanks for reading the article.

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

Ramsin

Web Developer

United States United States

Member

There is nothing I can say, except that I must learn a lot.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUseful Attachments Pinmemberjdmitchell19802:02 10 Nov '06  
GeneralMy idea and suggestion PinmemberJeff Varszegi16:02 1 May '04  
Generalfile not exist Pinmemberlhjlr19:10 29 Apr '04  
GeneralRe: file not exist PinmemberMartin Lundberg2:48 1 May '04  
GeneralRe: file not exist PinmemberRamsin11:58 4 May '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 May 2004
Article Copyright 2004 by Ramsin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid