The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 5 of 5

Thread: Reading Child nodes from XML file -- C#

  1. #1

    Reading Child nodes from XML file -- C#

    Hi Friends,.

    I am trying to read a XML file and it looks like this..

    <book>
    <journal id="a">
    <english>fgsfdf</english>
    <french>sdsdfd</french>
    <chinese>dfsdd</chinese>
    </journal>
    <journal id="b">
    <english>ggggf</english>
    <french>jjjfd</french>
    <chinese>lllfsdd</chinese>
    </journal>
    </books>

    I am trying to read the child nodes under "journal id=2". Is there way to do it?....

    XmlDocument path = newXmlDocument();
    XmlNodeList abc = path.GetElementsByTagName("journal");

    How to fetch the journal@id=2 elements ?...

    Thanks.


  2. #2
    Quote Originally Posted by doggy1982 View Post
    Hi Friends,.

    I am trying to read a XML file and it looks like this..

    <book>
    <journal id="a">
    <english>fgsfdf</english>
    <french>sdsdfd</french>
    <chinese>dfsdd</chinese>
    </journal>
    <journal id="b">
    <english>ggggf</english>
    <french>jjjfd</french>
    <chinese>lllfsdd</chinese>
    </journal>
    </books>

    I am trying to read the child nodes under "journal id=2". Is there way to do it?....

    XmlDocument path = newXmlDocument();
    XmlNodeList abc = path.GetElementsByTagName("journal");

    How to fetch the journal@id=2 elements ?...

    Thanks.


    what you woudl more do is..

    Code:
     XmlDocument doc = new XmlDocument()
    doc.Load(filepath);
    XmlNode = doc.SelectNode("/journal[@id=2]/");
    that will get you the node with it, in the event there are multiple, you will want doc.SelectNodes... but basically you provide an xpath and it does it for you. If you are using c# and .net3, you can use XDocument, which looks like

    Code:
     XDocument doc = XDocument.Load(XmlReader.Create(filepath));
    XElement myelement = doc.Decentents(XName.Get("/journal")).Where(n=>n.Attribute(XName.Get("id")).Value == 2).FirstorDefault();
    what that will do is get all the nodes named journal, then return an ienumerable for you to iterate through looking for the id=2. The first or default will return the first item, or if there are none, it returns null instead.

  3. #3
    Quote Originally Posted by bluefootedpig View Post
    what you woudl more do is..

    Code:
     XmlDocument doc = new XmlDocument()
    doc.Load(filepath);
    XmlNode = doc.SelectNode("/journal[@id=2]/");
    that will get you the node with it, in the event there are multiple, you will want doc.SelectNodes... but basically you provide an xpath and it does it for you. If you are using c# and .net3, you can use XDocument, which looks like

    Code:
     XDocument doc = XDocument.Load(XmlReader.Create(filepath));
    XElement myelement = doc.Decentents(XName.Get("/journal")).Where(n=>n.Attribute(XName.Get("id")).Value == 2).FirstorDefault();
    what that will do is get all the nodes named journal, then return an ienumerable for you to iterate through looking for the id=2. The first or default will return the first item, or if there are none, it returns null instead.


    Orh...I am sorry here.. i think i posted a bit wrong.... my actual problem is as below...
    <book>
    <A>hello</A>
    <journal>
    <english>fgsfdf</english>
    <french>sdsdfd</french>
    <chinese>dfsdd</chinese>
    </journal>
    <A>bye</A>
    <journal>
    <english>ggggf</english>
    <french>jjjfd</french>
    <chinese>lllfsdd</chinese>
    </journal>
    </books>

    I want to fetch the <journal> node under "<A>bye</A>"....
    how can i fetch the nodes and what criteria should i use?.....
    thnx mate....

  4. #4
    not much more difficult. You just need to quality it, which i'm having difficulty remember the xpath.

    it is the same thing with the .SelectSingleNode, but what you want to do is something like...

    doc.SelectSingleNode("/book[A='hello']/journal")


    that should be the xpath, although i'm not positive on it. I can try to look more but you might have to do some researching on xpathing in order to find the exact xpath that you want. That basically says from the root node, select books that have an a element that equals 'hello' and then of those, show me the journals.

  5. #5
    There are two tutorials on the site that go over what you are trying to do as well:

    http://www.kirupa.com/net/reading_xml_directly_pg1.htm
    http://www.kirupa.com/net/readingXML_pg1.htm


    Great, now even Kirupa is { facebooking | twittering }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012