473,624 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looping through XML nodes

hello all,

I have a problem. I have an XML file like below one.I want to loop through
node by node and store or display the data .

Can anybody give me a sample piece of c# code?

Thanks & Regards,

Praveen

<?xml version="1.0" encoding="utf-8" ?>
<book id=1>

<name>abc</name>

<title>xyz</title>

</book>

<book id=2>

<name>pqr</name>

<title>lmn</title>

</book>

Nov 12 '05 #1
1 46122
First of all you have to wrap the code into a root element. I used <books>
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book id=1>
<name>abc</name>
<title>xyz</title>
</book>
<book id=2>
<name>pqr</name>
<title>lmn</title>
</book>
</books>

You can use an XmlReader, but I traverse all nodes. It gives me flexibility
with selecting only elements I need:

XmlDocument doc = new XmlDocument();
doc.Load("doc.x ml");
XmlElement root = doc.DocumentEle ment;
XmlNodeList nodes = root.SelectNode s("/books"); // You can filter elements
here using XPath
foreach (XmlNode node in nodes)
{
string name = node["name"].InnerText;
string title = node["title"].InnerText;
// Do whatever you want
Console.Write(" name\t{0}\title \t{1}", name, title);
}

"Praveen Naregal" <pg*******@hotm ail.com> wrote in message
news:u2******** ******@TK2MSFTN GP11.phx.gbl...
hello all,

I have a problem. I have an XML file like below one.I want to loop through
node by node and store or display the data .

Can anybody give me a sample piece of c# code?

Thanks & Regards,

Praveen

<?xml version="1.0" encoding="utf-8" ?>
<book id=1>

<name>abc</name>

<title>xyz</title>

</book>

<book id=2>

<name>pqr</name>

<title>lmn</title>

</book>

Nov 12 '05 #2

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

Similar topics

13
4150
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I get *9*. In the example table, there are 4 rows with 4 columns each in the tbody. I'd expect 4 child nodes for the table body with 4 children each. I get those 4 plus 5 alerts in Mozilla/Netscape. I get only those 4 in IE and Opera. Note that...
4
4475
by: Mark | last post by:
Hopefully I 'm missing something silly, but I can't find an easy way to loop all list items in a simple <ol>. I was hoping a for loop as shown below would be enough, however clicking "alert all" in the following example gives me all but the first <li> element. The alertfirst() function finds the first <li> with no problem. There are multiple lists on the page, so getElementsByTagName('li') is probably not the most elegant solution. Btw. my...
4
1975
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 transform them into HTML/DOM attributes.
5
1507
by: k.a.bouton | last post by:
I am trying to transform my XML to produce just a tree of the unique nodes and subnodes and am having no luck. this is the sample xml <MyRoot> <contact> <name>Jane Doe</name> <country>USA</country> </contact>
2
5423
by: Cappy | last post by:
I am writing an XML menu structure. I have the following XML file <MenuItems> <Item> <Name>Homepa5ge</Name> <URL>/index.aspx</URL> <Alt>Return to homepage</Alt> <Image>/images/navLocationOff.jpg</Image>
2
6481
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 XmlDocument object using LoadXml: <?xml version="1.0" encoding="UTF-8"?>
0
1083
by: david.k.land | last post by:
I have an XML document that I'm looping through and modifying as I go. Some nodes have to be deleted and I don't want to process their children after deleting the node. If my XML structure looks like this: <top> <elem> <elem>first level2</elem> </elem>
14
6003
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about using 4 nested for-next loops, but I seem to be having trouble working out the logic to validate the octets in the beginning and ending address so the range works correctly. i.e. 172.16.1.1/172.20.1.1 should loop 172-172,16-20,1-254,1-254...
67
2645
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 file: <Scoreboard> <Events> <Event ID="001212" Name="EventName1" /> <Event ID="001213" Name="EventName2" /> <Event ID="001214" Name="EventName3" /> <Event ID="001215" Name="EventName4" />
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.