Click here to Skip to main content
Click here to Skip to main content

Manipulate XML data with XPath and XmlDocument (C#)

By , 4 Feb 2005
 

Sample Image

Introduction

Based on a section of easy-to-read XML source data, I'll show you how to select and locate XML nodes and navigate through them using XPathNavigator and XPathNodeIterator. I will provide a few straightforward samples about XPath expression with which you could follow without difficulty. In the last part, there is some sample code to update, insert and remove XML nodes.

Some Concepts

  • XML - Extensible Markup Language, describe data structures in text format and with your own vocabularies, which means it does not use predefined tags and the meaning of these tags are not well understood.
  • XSL - Extensible Stylesheet Language, is designed for expressing stylesheets for XML documents. XSL is to XML as CSS is to HTML.
  • XML Transformation - is a user-defined algorithm that transforms a given XML document to another format, such as XML, HTML, XHTML. The algorithm is described by XSL.
  • XSLT - is designed for use as part of XSL, transforming an XML document into another XML document, or another type of document that is recognized by a browser, like HTML or XHTML. XSLT uses XPath.
  • XPath - is a set of syntax rules for defining parts of an XML document.

To keep this article simple and clear, I'll break it down into two parts, and put XSL, XSLT to my next article.

Using the code

Here is the source XML data:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>10.0</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.90</price>
  </cd>
</catalog>

If you want to select all of the price elements, here is the code:

using System.Xml;
using System.Xml.XPath;
....
string fileName = "data.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr; 
expr = nav.Compile("/catalog/cd/price");
XPathNodeIterator iterator = nav.Select(expr);

// Iterate on the node set
listBox1.Items.Clear();
try
{
  while (iterator.MoveNext())
  {
     XPathNavigator nav2 = iterator.Current.Clone();
     listBox1.Items.Add("price: " + nav2.Value);
  }
}
catch(Exception ex) 
{
   Console.WriteLine(ex.Message);
}

In the above code, we used "/catalog/cd/price" to select all the price elements. If you just want to select all the cd elements with price greater than 10.0, you can use "/catalog/cd[price>10.0]". Here are some more examples of XPath expressions:

/catalog selects the root element
/catalog/cd selects all the cd elements of the catalog element
/catalog/cd/price selects all the price elements of all the cd elements of the catalog element
/catalog/cd[price>10.0] selects all the cd elements with price greater than 10.0
starts with a slash(/) represents an absolute path to an element
starts with two slashes(//) selects all elements that satisfy the criteria
//cd selects all cd elements in the document
/catalog/cd/title | /catalog/cd/artist selects all the title and artist elements of the cd elements of catalog
//title | //artist selects all the title and artist elements in the document
/catalog/cd/* selects all the child elements of all cd elements of the catalog element
/catalog/*/price selects all the price elements that are grandchildren of catalog
/*/*/price selects all price elements which have two ancestors
//* selects all elements in the document
/catalog/cd[1] selects the first cd child of catalog
/catalog/cd[last()] selects the last cd child of catalog
/catalog/cd[price] selects all the cd elements that have price
/catalog/cd[price=10.90] selects cd elements with the price of 10.90
/catalog/cd[price=10.90]/price selects all price elements with the price of 10.90
//@country selects all "country" attributes
//cd[@country] selects cd elements which have a "country" attribute
//cd[@*] selects cd elements which have any attribute
//cd[@country='UK'] selects cd elements with "country" attribute equal to 'UK'

To update a cd node, first I search out which node you are updating by SelectSingleNode, and then create a new cd element. After setting the InnerXml of the new node, call ReplaceChild method of XmlElement to update the document. The code is as follows:

XmlTextReader reader = new XmlTextReader(FILE_NAME);
XmlDocument doc = new XmlDocument(); 
doc.Load(reader);
reader.Close();

//Select the cd node with the matching title
XmlNode oldCd;
XmlElement root = doc.DocumentElement;
oldCd = root.SelectSingleNode("/catalog/cd[title='" + oldTitle + "']");

XmlElement newCd = doc.CreateElement("cd");
newCd.SetAttribute("country",country.Text);

newCd.InnerXml = "<title>" + this.comboBox1.Text + "</title>" + 
        "<artist>" + artist.Text + "</artist>" +
        "<price>" + price.Text + "</price>";

root.ReplaceChild(newCd, oldCd);

//save the output to a file
doc.Save(FILE_NAME);

Similarly, use InsertAfter and RemoveChild to insert and remove a node, check it out in the demo. When you run the application, make sure that "data.xml" is in the same directory as the EXE file.

Points of Interest

Anyway, XmlDocument is an in-memory or cached tree representation of an XML document. It is somewhat resource-intensive, if you have a large XML document and not enough memory to consume, use XmlReader and XmlWriter for better performance.

History

Version 1.0, it's my first article on CP, I expect there are many flaws. The XML source data and the knowledge comes from the web and MSDN, I just wrote a demo app to show them. No copyright reserved.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michael Chao
Canada Canada
Member
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMember 806697717 Apr '13 - 9:26 
The single most useful page I have used for my job.
QuestionWord document XMLmemberMember 984829019 Feb '13 - 14:27 
Hi,
 
I wanna use the same code for a word document, but as the tags have a qualifier (like ), it doesn't work.
If I put it gives this error: "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."
And if I just put , there is no error but it doesn't go through the tags.
 
Any suggestion?
Thanks
Questionhaving some issue for if root as <catalog xmlns="xyzvalue"> [modified]memberhemantrautela29 Jan '13 - 22:53 
HI,
 
If as in your example xml, I modify it as
to
 
then I cant read using xpath
oldCd = root.SelectSingleNode("/catalog/cd");
 
I am getting not error but it not read inner node also..
error message(Enumeration yielded no results)
 
works for below code
XmlNodeList oldCd = doc.GetElementsByTagName("title");

modified 30 Jan '13 - 6:33.

QuestionLike to access the XML of this formatmemberplatso_58824 Jul '12 - 22:23 
Hello i would like to parse the below XML file:
Can i get code snippet in where in i like to get the values 'P1', 'P2' when i query for the nodes "Display\PROCESSES\PROCESSTOP" and subsequently i like to get the values 'f1.doc' and 'f1.txt' when i query "Display\FILES\WINDOWS"/
 
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
   <Display>
    <PROCESSES>
      <PROCESSSTOP>P1</PROCESSSTOP>
      <PROCESSSTOP>P2</PROCESSSTOP>
      <PROCESSSTOP>P3</PROCESSSTOP>
      <PROCESSSTOP>P4</PROCESSSTOP>
      <PROCESSSTOP>P5</PROCESSSTOP>
      <PROCESSSTOP>P6</PROCESSSTOP>
      <PROCESSSTART>%ProgramFiles%\Windows Sidebar\sidebar.exe*/D "%ProgramFiles%\Windows Sidebar" /NORMAL "%ProgramFiles%\Windows Sidebar\sidebar.exe</PROCESSSTART>
    </PROCESSES>
    <SERVICES>
      <SERVICESTOP>SS1</SERVICESTOP>
      <SERVICEDELETE>SD2</SERVICEDELETE>
      <SERVICEDELETE>SD3</SERVICEDELETE>
      <SERVICEDELETE>SD4</SERVICEDELETE>
      <SERVICESTART>ss2</SERVICESTART>
    </SERVICES>
    <FILES>
      <WINDOWS>
        <FILE>f1.doc</FILE>
        <FILE>f2.txt</FILE>
      </WINDOWS>
      <SYSTEM32>
        <FILE>f1.dll</FILE>
        <FILE>f2.dll</FILE>
      </SYSTEM32>
      <SYSWOW64>
        <FILE>f1.log</FILE>
        <FILE>f2.log</FILE>
      </SYSWOW64>
    </FILES>
    <DIRECTORIES>
      <DIRECTORY>%HOMEDRIVE%\DIR1\</DIRECTORY>
      <DIRECTORY>%LOCALAPPDATA%\DIR2\</DIRECTORY>
    </DIRECTORIES>
    <REGISTRY>
      <HKEY_CLASSES_ROOT>
        <KEY>
          <RKEY></RKEY>
          <KVAL>KeyVal1</KVAL>
          <KVAL>KeyVal2</KVAL>
        </KEY>
      </HKEY_CLASSES_ROOT>
      <HKEY_CURRENT_USER>
        <KEY>
          <RKEY>Software\</RKEY>
          <KVAL>MyCompany</KVAL>
        </KEY>
      </HKEY_CURRENT_USER>
      <HKEY_LOCAL_MACHINE>
        <KEY>
          <RKEY>Software\Classes\</RKEY>
          <KVAL>LMKeyVal1</KVAL>
          <KVAL>LMKeyVal2</KVAL>
        </KEY>
        <KEY>
          <RKEY>Software\Classes\CLSID\</RKEY>
          <KVAL>{4242B984-6C36-4FC8-8626-DE5E8B11886D}</KVAL>
          <KVAL>{5E2121EE-0300-11D4-8D3B-444553540000}</KVAL>
        </KEY>
        <KEY>
          <RKEY>Software\Classes\Wow6432Node\CLSID\</RKEY>
          <KVAL>{0A9BD4EB-DED5-4DF0-BAF6-2CEA23F57261}</KVAL>
        </KEY>
      </HKEY_LOCAL_MACHINE>
    </REGISTRY>
  </Display>
</ROOT>

GeneralMy vote of 4memberItz.Irshad8 Jul '12 - 20:35 
Hi Michael,
Very Usefull Information.
Keep it up.
Thanks
GeneralMy vote of 4memberanver sadat16 May '12 - 1:41 
ggggggggggg
QuestionXml data with XPath and XmlDocumentmemberFistum Mekuria25 Apr '12 - 4:49 
Hello Michael,
 
I am currently working on a WinForm application to retriever information from the Xml and display that in the GUI. I am new for Xml and Xpath. I was wondering if you can guide me.
 
Here is the Xml
 

<head>

<title>
NOAA's National Weather Service Forecast by 24 Hour Period
</title>
meteorological
forecast
2012-04-23T15:34:17Z


http://graphical.weather.gov/xml/

Meteorological Development Laboratory
Product Generation Branch

http://www.nws.noaa.gov/disclaimer.html
http://www.weather.gov/
http://www.weather.gov/images/xml_logo.gif
http://www.weather.gov/feedback.php

</head>


point1



http://forecast.weather.gov/MapClick.php?textField1=40.39&textField2=-74.11


k-p24h-n1-1
2012-04-24T06:00:00-04:00
2012-04-25T06:00:00-04:00


k-p12h-n2-2
2012-04-24T06:00:00-04:00
2012-04-24T18:00:00-04:00
2012-04-24T18:00:00-04:00
2012-04-25T06:00:00-04:00


k-p1d-n1-3
2012-04-24T06:00:00-04:00
2012-04-25T06:00:00-04:00



Daily Maximum Temperature
60


Daily Minimum Temperature
41


12 Hourly Probability of Precipitation
32
17


Weather Type, Coverage, and Intensity





Conditions Icons

http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs30.jpg



Watches, Warnings, and Advisories





 
in a button click I would like to display the temperature of the day max and min. This is how I did it, but it's not working. Please help.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
 
namespace Test
 
{
 

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
 
}
private XmlDocument doc;
private XmlElement root;
private XmlElement currentTemperature;
private const string PATH = @"..\..\dwmlByDay.xml";
private int current = 0;
private int max;
 
private void Form2_Load(object sender, EventArgs e)
{
doc = new XmlDocument();
doc.Load(PATH);
 
//Get root element
root = doc.DocumentElement;
 
//Determine maximum possible index
max = root.GetElementsByTagName("Temperature").Count - 1;
 
//Get the record at the current index
currentTemperature = (XmlElement)root.ChildNodes[current];
 
//Show the record information
ShowDetails(currentTemperature);
}
 
private void ShowDetails(XmlElement currentTemperature)
{
throw new NotImplementedException();
}
 

 

private void button1_Click(object sender, EventArgs e)
{
current = 0;
currentTemperature = (XmlElement)root.ChildNodes[current];
ShowDetails(currentTemperature);
}
 
private void button2_Click(object sender, EventArgs e)
{
current = max;
currentTemperature = (XmlElement)root.ChildNodes[current];
ShowDetails(currentTemperature);
}
 

}
 

}
GeneralMy vote of 4memberAmit kumar pathak31 Mar '12 - 5:58 
Gud Xpath information
Question[My vote of 1] Namespace attribute breaks your examplemembertroybooth17 Feb '12 - 3:43 
Put a namespace attribute in your example (which is very common) and your example does not loop though nodes
Questionvote 5membertiktaktoe18 Jul '11 - 20:00 
vote 5 great !
GeneralMy vote of 5memberYSDen12322 Jun '11 - 22:39 
clear, shortly
GeneralMy vote of 5membersailu sirish6 May '11 - 0:01 
thanks it helped me a lot to do manipulations on xml file
GeneralMy vote of 1memberMilos Culic21 Dec '10 - 23:19 
rgdxcvfdxv
GeneralMy vote of 5memberXtErMiNaToR10224 Nov '10 - 1:38 
Brilliant article! Easy to follow and not a yawn to read.
GeneralMy vote of 5memberDell.Simmons28 Oct '10 - 15:01 
Easy to understand and apply.
GeneralGreat for beginnersmemberumerjafer30 Sep '10 - 7:47 
This is a great article for beginners to Xpath and the table with all the examples helps out a lot.
GeneralXML and SQL 2005/2008 Express with an XML fieldmemberlarry Williams31 Aug '10 - 13:15 
I found your post and example very informative and very useful. However, I am working on a project where I think we(the project) would be better served to use XML as a field in a data base table. The use (without telling what my company is doing) would be analogous to a proficiency test for students. For example say you are going to take a test of addition for all the the values of 1 (1 - 10) So the application will create the XML file or string with the root node being the <Test></Test> with a sibling node being <question>QuestionNum#</question> and a series of sibling nodes <problem>1 + 1</problem><solution>2</solution><answer>3</answer><durationtoanswer>10</durationtoanswer>
 
so I envision the xml as:
<test>
<question questnum = "1">
<problem> 1 + 1 </problem>
<solution> 2 </soltuion>
<answer> 3 </answer>
<durationtoanswer time = "seconds"> 10 </durationtoanswer>
</question>
<question questnum = "2">
<problem> 1 +2 </problem>
<solution> 3 </soltuion>
<answer> 3 </answer>
<durationtoanswer time = "seconds"> 5 </durationtoanswer>
</question>
<test>
 

So the question is how can I extract the data from the database (row/column) into either a xml doc, or a dataset with a corresponding xsd (if necessary) so it can be used to test, update (answer) and write back to the database as a completed test?
 
I apologize in advance for being so obtuse
GeneralAwesome!membererat1233 Jun '10 - 4:58 
Thanks, this really helped me a lot!
GeneralGet value of an element on the basis of two attribute valuesmemberBrad North30 Apr '10 - 2:04 
<catalog>
  <cd country="USA" Id=1>hello USA </cd>
  <cd country="China" Id=11>hello China</cd> 
</catalog>
 
for the above xml, I would like to get the value where country is USA and Id=1.
I tried XPath like "//cd[@country='USA' | @Id=1"
its not working.. can anyone help me.
Thanks in advance.
GeneralPlease go thru another good example.membershijoforum23 Oct '09 - 5:28 
You can find another method and compare it.. http://www.tctcworld.com/dotnet/viewtopic.php?f=1&t=63[^]
GeneralThank youmemberMiko_chan29 Jun '09 - 11:43 
Thank Michael, your example is simple and easy to follow which is great for us who don't know XPATH
GeneralCan't figure out how to read my xmlmembersraelgaiznaer14 Jun '09 - 16:43 
Hello All,
 
I have a sample xml below:
 
<?xml version="1.0"?>
<DownloadListData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Entries>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="DownloadListEntries">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Guid" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
                  <xs:element name="Title" type="xs:string" minOccurs="0" />
                  <xs:element name="Filename" type="xs:string" minOccurs="0" />
                  <xs:element name="Size" type="xs:string" minOccurs="0" />
                  <xs:element name="Comment" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
        <xs:unique name="Constraint1" msdata:PrimaryKey="true">
          <xs:selector xpath=".//DownloadListEntries" />
          <xs:field xpath="Guid" />
        </xs:unique>
      </xs:element>
    </xs:schema>  
	<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
      <NewDataSet>
        <DownloadListEntries diffgr:id="DownloadListEntries1" msdata:rowOrder="0">
          <Guid>d6161e11-69ed-41df-9959-e45826efedda</Guid>
          <Title>AS_AM_SOP0001G QuickCard BEA Operation</Title>
          <Filename>AS_AM_SOP0001G QuickCard BEA Operation.xls</Filename>
          <Size>1.5 MB</Size>
          <Comment>BEA</Comment>
        </DownloadListEntries>
        <DownloadListEntries diffgr:id="DownloadListEntries2" msdata:rowOrder="1">
          <Guid>3a743b6b-db19-47f0-b36a-9cfc1943ed76</Guid>
          <Title>AS_AM_SOP0001P ALSB How to remove hung sessions</Title>
          <Filename>AS_AM_SOP0001P ALSB How to remove hung sessions.doc</Filename>
          <Size>3.3 MB</Size>
          <Comment>BEA</Comment>
        </DownloadListEntries>
        <DownloadListEntries diffgr:id="DownloadListEntries3" msdata:rowOrder="2">
          <Guid>f36160fd-bcc3-4c8a-a7c4-158f71c1bb15</Guid>
          <Title>AS_SUP8001Z DCv2 Master List of Documents</Title>
          <Filename>AS_SUP8001Z DCv2 Master List of Documents.xls</Filename>
          <Size>0.1 MB</Size>
          <Comment>BEA</Comment>
        </DownloadListEntries>
      </NewDataSet>
	</diffgr:diffgram>
  </Entries>
</DownloadListData>
 
I am trying to get all filenames element using the following:
 
            doc = new XPathDocument(path);
            nav = doc.CreateNavigator();
            XPathExpression apps = nav.Compile("/DownloadListData/Entries/NewDataSet/DownloadListEntries/Filename");
            XPathNodeIterator iterator = nav.Select(apps);
            comboBox2.Items.Clear();
            comboBox2.Items.Add("All Applications/Templates");
            try
            {
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    comboBox2.Items.Add(nav2.Value);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show("Problems with loadDir() function: " + e.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
However whenever I load my application the combo box is not populated with the filenames from the xml. Am I doing something wrong? Thanks! Smile | :)
QuestionGet elements that contain a certain stringmemberfcp14 Jun '09 - 1:54 
Hi,
 
Can you help me in this one. I have the followint structure:
 
<pre>&lt;ViatecService lr="218862967" ac="01LX12" pd="1221" oper="" ori="APIWEB" tipoPedido="ADSL"&gt;
     &lt;Timestamp&gt;20090604114831&lt;/Timestamp&gt;
     &lt;Cobertura servico="ADSL"&gt;1&lt;/Cobertura&gt;
     &lt;Cobertura servico="ADSL2MAIS"&gt;1&lt;/Cobertura&gt;
     &lt;Cobertura servico="ADSL2MAISM"&gt;1&lt;/Cobertura&gt;
     &lt;Cobertura servico="ETHERNET"&gt;1&lt;/Cobertura&gt;
     &lt;Cobertura servico="IPTV"&gt;1&lt;/Cobertura&gt;
     &lt;Viatec sistema="XXX"&gt;1&lt;/Viatec&gt;
     &lt;Viatec sistema="ATNADSL"&gt;1&lt;/Viatec&gt;
     &lt;Viatec sistema="ADSL1MB"&gt;1&lt;/Viatec&gt;
     &lt;Viatec sistema="DEMO123"&gt;1&lt;/Viatec&gt;</pre>
(...)
 
And i want to select all "viatec" tags whose element sistema contains ADSL...
 
I've tried this but didn't worked out.
 
'xmlDoc.SelectNodes("//ViatecService/Viatec[@sistema contains(text,'ADSL256')]")' threw an exception of type 'System.Xml.XPath.XPathException'     int {System.Xml.XPath.XPathException}
 

Best regards.
Questionhi guysmembersuhaib198220 May '09 - 3:07 
the original question was about running xpath in CE device
the program works fine in the pc
but no in the CE project coz the library or the namespace of xpath dont have all the classes needed
Questionhow to create xml file using xpaths?memberkumarrajt4 May '09 - 21:13 
hi,
I have few xpaths in one column of excel sheet like
Group/GroupID
Group/RatingDate
Group/CreatedDate
Group/GroupName
Group/CreatedBy
Group/SourceApplication .....
There are many more like this and there are many more subnodes to it
and my requirement is to create xml file using using these xpaths.
how to do this? is this possible to create xml file using these xpaths.
Plz suggest me.
 
Raj

GeneralExtracting Attributes of nodesmembernaeem.akram.malik29 Jan '09 - 3:27 
I got this XML

<header name="Event" value="Ringing" />
<header name="ABC" value="XYZ" />
<header name="DEF" value="JKL" />
<header name="GHI" value="PQR" />

 
To get the very first node which contains name="Event" i use
"//Event/header[@name='Event']" for XPathExpression and it works fine.
 
But afterwards, i can't figure out how to make a XMLNode from XPathNodeIterator.Current and use its attributes.
This is a simple, useful, and nice article... know "Simple is the best".
Thanks
GeneralXpathmemberabhilashj12530 Dec '08 - 22:36 
compare 2 xml files ..which includes comparing its elements and attributes.just give some help on that
GeneralTHank youmemberSanShark6 Dec '08 - 1:25 
Thank you
 
sanshark.com
GeneralMy vote of 2memberDheerajJain19782 Dec '08 - 23:59 
lacks examples
GeneralMy vote of 5memberIndrora22 Nov '09 - 4:04 
Has examples that perfectly pertain to the topic.
 
----
Morgan Gangwere
Lead programmer, Unknown Software
 
"Pinky, are you thinking what im thinking?"
"I Dunno brain, how many licks DOES it take to get to the tootsie roll center of a tootsie pop?"
"You want me to calculate that? or should we take over the world?"
"ooh! OooooOOOooH! lets find out!"

GeneralRe: My vote of 5memberDell.Simmons28 Oct '10 - 15:01 
examples are great.
GeneralTHANK YOU SSOOOO MUCH...!!!memberMelu3 Apr '08 - 10:26 
simply gr8.....helped me a lot...excellent article..thanks you sooo much....
 
Programming is a Waste..
Trust, Copy Paste.

GeneralBest Article on XML Serialization and Selecting NodememberSamTheGreat15 Mar '08 - 2:44 
Best Article on XML Serialization and Selecting Node
Really this has helped we a lot and i wish all the User to Study this
QuestionC# code for getting value from XML filesussRitesh Gupta6 Dec '07 - 20:19 
how to get TestGroupID from XML, which I get from following query
 
SELECT TestGroupID FROM TestGroup
WHERE
Description = @groupName
AND
UserID=@UserID
 
XML will look like
<testgroupdata>
<testgroup>
<testgroupid> 1 </testgroupid>
<testcaseid> 2 </testcaseid>
<description> ABC </description>
</testgroup>
<testgroup>
...........
..........
.........
</testgroup>
</testgroupdata>
Questionwhats the difference between using ReplaceChild and Setting InnerXml of the oldNode?memberAnandChavali21 Aug '07 - 20:26 
whats the difference between using ReplaceChild and Setting InnerXml of the oldNode?
 
Setting the InnerXml of the OldCD node (the code smaple provided towards the end of the article) should do right? then why to use Replace child? I am a new bee to .NET. So please correct/explain me
 
Thanks and Regards,
Anand.

AnswerRe: whats the difference between using ReplaceChild and Setting InnerXml of the oldNode?member-Rufus-30 Sep '08 - 8:13 
That was exactly my question. I just wrote some code to do it by setting the inner XML on the old node. Is ReplaceChild preferable for some reason?
 
Also, thanks for a great, short, sweet article on the subject. Not too detailed, but containing just enough to get the non-experienced on their way. I could have used this yesterday!
GeneralNice JobmemberMike Hankey25 Mar '07 - 13:22 
Got a lot out of the article...
 
Thanx,
Mike
 
Everybody gotta be somebody

QuestionRestricted editing in xmlmemberAnash P Oommen13 Sep '06 - 2:30 
Does anyone know of any class that allows the user to edit the attribute/values of the underlying xmlelement, but prevent him from adding/removing siblings/childnodes/new attributes? What I am trying to do is to use XML as a replacement for a struct, so the user can edit the values of existing fields, but cannot change the underlying xml structure.
 
Thanks in advance
Anash
GeneralSelect Distinct Attributes or NodesmemberA55imilate29 Jun '06 - 1:22 
I've been trying to populate a combo box with distinct items from an XML. Originaly i was retrieving the nodes and looping around the combo to see if the item already existed in the combo box. This , I thought was very inefficient.
 
searching for select distinct xpath wasnt turning up any sensible results. After a lot of hunting i managed to do it and thought this was a good place to share it.
 
Using the code in this article I added a textbox and a button to the form so I could play with different queries.
 
The button calls GetElements passing it the text in the text box (an xpath query). no rocket science here.
Edit the following line in GetElements method to return a clearer message in the listview
//listBox1.Items.Add("price: " + nav2.Value);
listBox1.Items.Add(nav2.Name + ": " + nav2.Value);
 
here are some examples of xpath queries that return distinct values from the xml provided in this editorial.
 

//cd[not(@country=preceding-sibling::cd/@country)]/@country
 
//cd[not(artist=preceding-sibling::cd/artist)]/artist
 
the first returns distinct countries using the attribute country in the cd element
the second returns distinct artists using the artist element.
 
I hope this helps someone out there
 

 

QuestionRe: Select Distinct Attributes or Nodesmember~MyXa~3 Aug '07 - 9:16 
How to select distinct artists if there is a <catalogs> root tag and many <catalog> under <catalogs>?
 
Thanks.
AnswerRe: Select Distinct Attributes or NodesmemberA55imilate5 Aug '07 - 20:33 
Can you post the xml?
GeneralRe: Select Distinct Attributes or Nodesmember~MyXa~6 Aug '07 - 1:09 
<searchResults>
 
   <searchResult>
      <vehicle>
         <options>
            <option>CD Player</option>
            <option>Immobilizer</option>
         </options>
      </vehicle>
   </searchResult>
 
   <searchResult>
      <vehicle>
         <options>
            <option>Power Steering</option>
            <option>CD Player</option>
         </options>
      </vehicle>
   </searchResult>
 
</searchResults>
 
How to select distinct option?
AnswerRe: Select Distinct Attributes or Nodesmember~MyXa~6 Aug '07 - 21:17 
//*/option[not(.=preceding::*/option)]
GeneralNamespaces and nodes not on the rootmembertitltn2113 Dec '05 - 10:52 
Thanks for the article. It guided me along today as I edited my machine.config file. I had two big hurdles that I had to overcome, and wanted to share my experience. Namespaces such as xmlns="urn:schemas-microsoft-com:asm.v1" and when nodes being replaced are a bit deeper than the example.
 

MACHINE.CONFIG Example

<configuration>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
   <dependentAssembly>
    <assemblyIdentity name="myAssembly" publicKeyToken="d989999999999999" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="1.0.0000.00000" />
   </dependentAssembly>
...>>

 

Here is how i grabed the dependentAssembly node using name spaces

//build the namespace manager
//http://www.adlnet.org/forums/messageview.cfm?catid=15&threadid=1247&enterthread=y
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bindings","urn:schemas-microsoft-com:asm.v1");
 
//Get Current Binding Node using NameSpaceManager
sNodeXPath = "//bindings:assemblyIdentity[@name='myAssembly']/parent::*";
oldBinding = root.SelectSingleNode(sNodeXPath, nsmgr);

 
and later to do a replace of a node, I had to back track a bit to be at the proper insertion point

//Replace new binding with old binding
XmlNode nodeInsert = root.SelectSingleNode(sNodeXPath, nsmgr);
nodeInsert.ParentNode.ReplaceChild(newBinding, oldBinding);

 
this resolves an error similiar if we changed the example xlm wrap everything with "myRoot":

<?xml version="1.0" encoding="ISO-8859-1"?>
<myRoot>
 <catalog>
  <cd country="USA">
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <price>10.90</price>
  </cd>
  <cd country="UK">
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <price>10.0</price>
  </cd>
  <cd country="USA">
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <price>9.90</price>
  </cd>
 </catalog>
</myRoot>

 
I hope this might help others as this example helped me out today.
GeneralXPath DataSetsussAnonymous12 Oct '05 - 16:38 
Hi Michael, nice work Wink | ;)
 
But I have some trouble to make dataset as the result of theXPath statement.
as we know if we use :
 
DataSet ds = new DataSet();
ds.ReadXml("C://data.xml");
 
just that simple, it will return a dataset consist of all parts of data.xml
 
but how if i dont want to return all data, just partially data filtered by the XPath statement.
 
Sorry if my english is very awful
 
Best Regards..
AnswerRe: XPath DataSetmemberGustavo Leal28 Feb '06 - 2:45 
Did you try using dataview?
GeneralSchema Locationmembermeir_rivkin26 Jul '05 - 2:04 
hi Michael,
 
thanks for a great article.
i was trying using your code in my application, however, it seems that everytime i add SchemaLocation into my XML file it doesn't work.
how do i overcome this problem?

GeneralRe: Schema LocationmemberMember 376457513 Feb '09 - 2:11 
ive got the same problem Frown | :-(
GeneralAdditional Xpath Tutorial LinksussAnonymous6 Feb '05 - 18:32 
http://www.w3schools.com/xpath/default.asp[^]
GeneralBasic approach works not alwaysmemberAlexander Turlov5 Feb '05 - 6:59 
Hi Michael. Good article, but I want to put some notices on topic you touched.
First, this approach that you described only works good if you don't use namespaces in your XML document. And actually it's exactly your case. If there are namespaces in your document you have to use much more sophisticated approaches. And in the real life, especially in business environment you always have XML documents containing not one but multiple different namespaces. To cover this topic you probably might want to add couple paragraphs about using XmlNamespaceManager.
Second, don't you think it's not a good approach to use an untyped XML document for accessing and manipulating information? Even it's not recommended to use standard datasets for that. The recommendation is to use typed datasets instead. That's why I am thinking to write a simple class that serves as a typed wrapper for the standard XML document.
 
Alexander Turlov
Software development consultant, MCP

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 4 Feb 2005
Article Copyright 2005 by Michael Chao
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid