473,385 Members | 1,478 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,385 software developers and data experts.

Looping through attributes

I need a sample that shows how can loop through the
attributes and display the data.(.net / c#)

<?xml version="1.0" encoding="UTF-8"?>
<riders>
<rider>
<prename id=>Lance</prename>
<surname>Armstrong</surname>
</rider>
<rider>
<prename>Roberto</prename>
<surname>Heras</surname>
</rider>
<rider>
<prename>George</prename>
<surname>Hincapie</surname>
</rider>
<Position>
<Element Operation="insert" remark="Test Add Remark1"/>
<Element Operation="insert" remark="Test Add Remark2"/>
</Position>
</riders>

Code:

XmlElement child2 = child.SelectSingleNode("ns:Element");
oper1 = child2.GetAttribute("Operation");

int i;
for ( i = 1; i < 3; i++)
{
if (oper1 == "insert")
{
remk = child2.GetAttribute("Child");
MessageBox.Show(remk);
}
}

This works fine but it only give me the first remark, it never goes to
the second remark. Also I have hard coded "< 3", but I don't know the
number of remarks will be in the document.
Nov 12 '05 #1
3 6833


Cheryl wrote:
I need a sample that shows how can loop through the
attributes and display the data.(.net / c#)

<?xml version="1.0" encoding="UTF-8"?>
<riders>
<rider>
<prename id=>Lance</prename>
<surname>Armstrong</surname>
</rider>
<rider>
<prename>Roberto</prename>
<surname>Heras</surname>
</rider>
<rider>
<prename>George</prename>
<surname>Hincapie</surname>
</rider>
<Position>
<Element Operation="insert" remark="Test Add Remark1"/>
<Element Operation="insert" remark="Test Add Remark2"/>
</Position>
</riders>

Code:

XmlElement child2 = child.SelectSingleNode("ns:Element");
oper1 = child2.GetAttribute("Operation");

int i;
for ( i = 1; i < 3; i++)
{
if (oper1 == "insert")
{
remk = child2.GetAttribute("Child");
MessageBox.Show(remk);
}
}

This works fine but it only give me the first remark, it never goes to
the second remark. Also I have hard coded "< 3", but I don't know the
number of remarks will be in the document.


Here is an example that loops through all <Element> elements that have
the id attribute Operation set to "insert" and then outputs the value of
the attribute named remark:

using System;
using System.Xml;

public class Test20040509 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040509.xml");
XmlNodeList nodeList = xmlDocument.SelectNodes(
"/riders/Position/Element[@Operation = 'insert']");
foreach (XmlNode node in nodeList) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(element.GetAttribute("remark"));
}
}
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2
XmlNodeList nodeList = xmlDocument.SelectNodes(
"/riders/Position/Element[@Operation = 'insert']")

Would the above nodeList change if my Xml looked as following:
<?xml version="1.0" encoding="UTF-8"?>
<UpdateRQ>
<riders>
<rider>
<prename>Lance</prename>
<surname>Armstrong</surname>
</rider>
<rider>
<prename>Roberto</prename>
<surname>Heras</surname>
</rider>
<rider>
<prename>George</prename>
<surname>Hincapie</surname>
</rider>
</riders>
<Position>
<Element Operation="insert" remark="Test Add Remark1"/>
<Element Operation="insert" remark="Test Add Remark2"/>
</Position>
</UpdateRQ>



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
Yes. The XPath expression included in your sample code looks for the very
first element in the document calles "riders", having a child element
"Position", having a child element named "Element", that has an attribute
named "Operation" with a value of "insert". If all of these conditions are
not met, then nothing will be matched... hence nothing is returned. Your
first element is now "UpdateRQ", so the XPath expression will not match
anything in your structure. Add the UpdateRQ element into your XPath
statement:

XmlNodeList nodeList =
xmlDocument.SelectNodes("/UpdateRQ/riders/Position/Element[@Operation =
'insert']")

--
Kirk Allen Evans
blogs.msdn.com/kaevans

-- This posting is provided "AS IS" with no warranties, and confers no
rights.

"cheryl hawes" <ch********@aol.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
XmlNodeList nodeList = xmlDocument.SelectNodes(
"/riders/Position/Element[@Operation = 'insert']")

Would the above nodeList change if my Xml looked as following:
<?xml version="1.0" encoding="UTF-8"?>
<UpdateRQ>
<riders>
<rider>
<prename>Lance</prename>
<surname>Armstrong</surname>
</rider>
<rider>
<prename>Roberto</prename>
<surname>Heras</surname>
</rider>
<rider>
<prename>George</prename>
<surname>Hincapie</surname>
</rider>
</riders>
<Position>
<Element Operation="insert" remark="Test Add Remark1"/>
<Element Operation="insert" remark="Test Add Remark2"/>
</Position>
</UpdateRQ>



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #4

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

Similar topics

2
by: biner | last post by:
Hello, I would like to be able to make a loop over a given class attributes. For example, with the following class definition : >>> class x: .... val1=1 .... val2=2 .... >>> print...
4
by: louissan | last post by:
Hi all, I'm coming across a problem, and really do not get where it comes from. The goal: to loop over attributes read from "object" nodes in an imported XML file/flow (via XMLHTTP) and...
1
by: Jon Bowlas | last post by:
HI all, I'm fairly new to python and programming in general so I was hoping someone here may be able to help me. Let me explain what the problem I'm having is: I am trying to parse the XML of...
2
by: Claire Reed | last post by:
Dear All, I am repeatedly encountering a problem whilst looping through XML Nodes and I am unsure as to what is going on and how I can get around it. I load the following XML document into an...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
4
by: musosdev | last post by:
Hi I need to extend the contents of the src element of the <link relcss tags in the <headsection of my masterpage. From code, how can I access the <link elements within the <head? Cheers
67
by: gator | last post by:
I am quite new to XML and posted a request for example code yesterday. Unfortunately, I did not do a very good job in explaining what I was looking for. Here is an example of a small piece of the...
2
by: li72 | last post by:
Hi. guys The following xslt stylesheet supposed to loop through the xml doc visiting each node, read and write its name and their attributes (userid, password). Actually it visits the only the first...
0
by: Rene Goris | last post by:
Hi all, I am trying to add meta:resourcekey's to database programmatically looping throug webcontrols, but i cann't read the 'meta:resourcekey' from the webcontrol. The 'meta:resourcekey' will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.