extracting an entire node from XML file

Ask a Question related to ASP.NET Web Services, Design and Development.

  1. #1

    Default extracting an entire node from XML file

    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 [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Smile Guest

  2. Similar Questions and Discussions

    1. Can't I insert an entire XML node?
      I'm trying to insert an entire XML node - it completely matches the other nodes in the XML Doc. But CF just gives me errors (An error occured while...
    2. Extracting an img file
      How do i extract the contents of an img file so i can view // empty the contents out? without burning due to it being a 3.1gig img file and i got...
    3. #25729 [Opn->Bgs]: $node->get_content() returns the entire content of all descendant nodes
      ID: 25729 Updated by: rrichards@php.net Reported By: fabiostt at libero dot it -Status: Open +Status: ...
    4. #25729 [NEW]: $node->get_content() returns the entire content of all descendant nodes
      From: fabiostt at libero dot it Operating system: any PHP version: 4.3.3 PHP Bug Type: DOM XML related Bug description: ...
    5. [PHP] Value for entire file
      > -----Original Message----- No, $_GET has always been superglobal, it just didn't exist before 4.1.0 -- so for PHP <4.1.0 it's: function...
  3. #2

    Default Re: extracting an entire node from XML file

    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
    ([url]http://www.codevoid.net/downloads.htm[/url] . 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 <smiley@developersdex.com>
    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 [url]http://www.developersdex.com[/url] ***
    >Don't just participate in USENET...get rewarded for it!
    Dominic Hopton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139