extracting an entire node from XML file

S

Smile

i have a file which i have to write in C#. i want to extract an entire
node by itself at once ( wherever it occurs in the file .. as )

<?xml version="1.0"?>
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>

Program Output
Root element of the doc is book
Total no of people : 3
First Name : Kiran
Last Name : Pai
Age : 22
First Name : Bill
Last Name : Gates
Age : 46
First Name : Steve
Last Name : Jobs
Age : 40


In java it says something like ...
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());


NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){


Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


Element firstPersonElement = (Element)firstPersonNode;

...........
....

Here in the same place of NodeList and all that .. what do i use in C#.
is there anything with the same functionality
??

Please someone out there help me on this.

Smile
 
D

Dominic Hopton

You should be able to use a similar method in C# - just look in the
..net SDK docs at accessing XML files using the XmlDocument Interface.

You may want to look at my RssEater sample
(http://www.codevoid.net/downloads.htm . In this sample I load an XML
document (RSS in this case) into an XMLDocument and then get the notes
out, almost exactly like your java code, except in c#.

If this is not what you are asking, could you provide a few more
details?

Yours,
Dominic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top