473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XML - get data

I've pasted a shortened XML file I have below. The path to the file is
strored in a string titled "myFile".

What would be the syntax for getting the crse_no and prog_type columns where
the sect_key = 176902?

Thanks in advance.
Mark

************ XML FILE BELOW **********

<?xml version="1.0" standalone="yes"?>
<CourseData>
<blah>
<cluster_cd>WB01</cluster_cd>
<sect_key>176901</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0017 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>A</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176902</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0018 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>B</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176903</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0019 </crse_no>
<crse_sec_no>002</crse_sec_no>
<prog_type>C</prog_type>
</blah>
</CourseData>
Nov 16 '05 #1
4 1159
[STAThread]
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
string file = @"C:\myfile.xml";
xd.Load(file);
XmlTextReader r = new XmlTextReader(file);

string strSectKey="176902";

while (r.Read())
{
if (r.NodeType == XmlNodeType.Element)
{
if ( r["sect_key"] == strSectKey)
{
Console.WriteLine(r["prog_type"] + ", " + r["crse_no"] + ", " +
r["sect_key"]);
}
}
}

Console.Read();
}

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
I've pasted a shortened XML file I have below. The path to the file is
strored in a string titled "myFile".

What would be the syntax for getting the crse_no and prog_type columns where the sect_key = 176902?

Thanks in advance.
Mark

************ XML FILE BELOW **********

<?xml version="1.0" standalone="yes"?>
<CourseData>
<blah>
<cluster_cd>WB01</cluster_cd>
<sect_key>176901</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0017 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>A</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176902</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0018 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>B</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176903</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0019 </crse_no>
<crse_sec_no>002</crse_sec_no>
<prog_type>C</prog_type>
</blah>
</CourseData>

Nov 16 '05 #2
you could also have used an XPath query

--- Nick

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
[STAThread]
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
string file = @"C:\myfile.xml";
xd.Load(file);
XmlTextReader r = new XmlTextReader(file);

string strSectKey="176902";

while (r.Read())
{
if (r.NodeType == XmlNodeType.Element)
{
if ( r["sect_key"] == strSectKey)
{
Console.WriteLine(r["prog_type"] + ", " + r["crse_no"] + ", " +
r["sect_key"]);
}
}
}

Console.Read();
}

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
I've pasted a shortened XML file I have below. The path to the file is
strored in a string titled "myFile".

What would be the syntax for getting the crse_no and prog_type columns

where
the sect_key = 176902?

Thanks in advance.
Mark

************ XML FILE BELOW **********

<?xml version="1.0" standalone="yes"?>
<CourseData>
<blah>
<cluster_cd>WB01</cluster_cd>
<sect_key>176901</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0017 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>A</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176902</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0018 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>B</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176903</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0019 </crse_no>
<crse_sec_no>002</crse_sec_no>
<prog_type>C</prog_type>
</blah>
</CourseData>


Nov 16 '05 #3

Mark,

You can do it like this:

string myKey = "176902";
XmlNode node1 = doc.SelectSingleNode(String.Format
("descendant::blah[@sect_key='{0}']", myKey));

Try to learn XPath as Nick suggested.
-----Original Message-----
I've pasted a shortened XML file I have below. The path to the file isstrored in a string titled "myFile".

What would be the syntax for getting the crse_no and prog_type columns wherethe sect_key = 176902?

Thanks in advance.
Mark

************ XML FILE BELOW **********

<?xml version="1.0" standalone="yes"?>
<CourseData>
<blah>
<cluster_cd>WB01</cluster_cd>
<sect_key>176901</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0017 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>A</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176902</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0018 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>B</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176903</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0019 </crse_no>
<crse_sec_no>002</crse_sec_no>
<prog_type>C</prog_type>
</blah>
</CourseData>
.

Nov 16 '05 #4
Thanks for kicking in with an actual bit of code, Chris. That makes the
answer much more complete.

--- Nick

"Chris Ongsuco" <an*******@discussions.microsoft.com> wrote in message
news:27****************************@phx.gbl...

Mark,

You can do it like this:

string myKey = "176902";
XmlNode node1 = doc.SelectSingleNode(String.Format
("descendant::blah[@sect_key='{0}']", myKey));

Try to learn XPath as Nick suggested.
-----Original Message-----
I've pasted a shortened XML file I have below. The path

to the file is
strored in a string titled "myFile".

What would be the syntax for getting the crse_no and

prog_type columns where
the sect_key = 176902?

Thanks in advance.
Mark

************ XML FILE BELOW **********

<?xml version="1.0" standalone="yes"?>
<CourseData>
<blah>
<cluster_cd>WB01</cluster_cd>
<sect_key>176901</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0017 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>A</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176902</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0018 </crse_no>
<crse_sec_no>001</crse_sec_no>
<prog_type>B</prog_type>
</blah>
<blah>
<cluster_cd>WB02</cluster_cd>
<sect_key>176903</sect_key>
<crse_desn>CLC </crse_desn>
<crse_no>0019 </crse_no>
<crse_sec_no>002</crse_sec_no>
<prog_type>C</prog_type>
</blah>
</CourseData>
.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
9
by: Tony Lee | last post by:
Some time a ago, on this newsgroup the following comments were made in recommending good references for Access (2003) >I used to recommend Dr. Rick Dobson's, "Programming Access <version>" for...
1
by: djozy | last post by:
Please, I want to insert data into SQL Server database. I know for this commmand: SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " + "Values ('string', 1)",...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
3
by: bbernieb | last post by:
Hi, All, Is it possible to access a variable inside of a data binding, without the variable being out of scope? (Note: On the DataBinder line, I get an error message that says "Name 'i' is...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
5
by: DC Gringo | last post by:
I am having a problem reading a simple update to the database. Basically I'm testing a small change to the pubs database -- changing the price of the Busy Executive's Database Guide from 19.99 to...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
0
by: Winder | last post by:
Computer Data Recovery Help 24/7 Data recovering tools and services is our focus. We will recover your data in a cost effective and efficient manner. We recover all operating systems and media....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.