extracting an entire node from XML file

Discussion in 'ASP .Net Web Services' started by Smile, Oct 9, 2003.

  1. Smile

    Smile Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Smile, Oct 9, 2003
    #1
    1. Advertising

  2. 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

    On Wed, 08 Oct 2003 23:25:15 -0700, Smile <>
    wrote:

    >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
    >
    >
    >*** Sent via Developersdex http://www.developersdex.com ***
    >Don't just participate in USENET...get rewarded for it!
     
    Dominic Hopton, Oct 9, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,470
  2. Replies:
    5
    Views:
    6,191
    Andy Dingley
    May 10, 2006
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,440
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    10
    Views:
    786
    Dimitre Novatchev
    Dec 1, 2007
  5. Replies:
    2
    Views:
    828
    Martin Honnen
    Mar 17, 2008
Loading...

Share This Page