10 Replies - 1960 Views - Last Post: 28 August 2012 - 06:22 AM Rate Topic: -----

#1 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 02:03 AM

Hi guys,

Complete novice here so please be patient. I work with XML on a daily basis but C# is something which i'm trying hard to learn as I know it will be useful in my job.

I've been playing with various methods and classes in the Xml namespace and i am just trying to perform some basic functions - for practice more than anything.

Firstly, i seem to be able to read my XML documents using XmlReader without too much trouble and can output to a console window (very basic stuff). I now want to find each occurance of the element "refdm" within the document and output just the content of the child elements of "refdm" (not the xml elements as well). So the following example is an extract of the XML (full file attached) and i basically want to pull the content of the text that is contained within the child nodes of the 'refdm' elements, and do this for each occurance of refdm, so that i have a nice list of all the refdms in the document.

<title>Location</title>
<para>There is a Shore Connection Box installed on each side of the
ship, each with its own compartment on Deck 4 at the RAS point. They
are connected on the Interconnectors between the Forward Starboard
Switchboard (HVSB1) and each of the Aft Switchboards (HVSB3 and HVSB4).
The compartments remain closed when the ship is at sea. For manufacturers
manual refer to data module <refdm><avee><modelic>CVF0000000QUE</modelic>
<sdc>AAA</sdc><chapnum>AD2</chapnum><section>0</section><subsect>5</subsect>
<subject>30</subject><discode>00</discode><discodev>00</discodev>
<incode>031</incode><incodev>A</incodev><itemloc>A</itemloc></avee>
</refdm>.</para>


With the following code i can output each occurance as required, but it also displays the xml tags of the child elements, which i don't want:

XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Parse;

            XmlReader xmlReader = XmlReader.Create("C:\\Documents and Settings\\S0035651\\Desktop\\XML_test.xml", settings);

            while (xmlReader.Read())
            {
                if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "refdm"))
                {
                    try
                    {

                        Console.WriteLine("DM Code: " + xmlReader.ReadInnerXml());

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                }

            }
                Console.ReadLine();


I'm aware that beacuse i'm using the ReadInnerXml method that i'm asking to display the xml elements as well but i cannot work out an alternative.

A foreach loop using XmlDocument seems like a sensible option but i can't seem to get that to work either. XmlDocument may be a better option but i get errors when it attempts to read the document.

As I said, complete novice with C#. Any pointers or advice would be appreciated.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: XmlReader - can't extract the content of child nodes

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 02:17 AM

hi,
I suggest using Xmldocument. you can get only the elements you want and then use them. for example:
           System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
           System.Xml.XmlNodeList xmlNodeList;
           xmlDoc.Load("c:\test.xml"); //Loads the document
           xmlNodeList = xmlDoc.GetElementsByTagName("somElementName"); //Select elements using his name
           foreach (System.Xml.XmlNode xmlNode in xmlNodeList)
           {
               //Loops all the elements in the node list
               MessageBox.Show(xmlNode.ChildNodes[0].InnerText); //if you have elements in the element using ChildNodes[0] we select the first one and display its text
               MessageBox.Show(xmlNode.InnerText);//if it dose not have any inner elements we just show the text
           }



good luck :)
Was This Post Helpful? 2
  • +
  • -

#3 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 02:48 AM

many thanks for your reply Raziel.

This is almost exactly what i'm looking for, but it is currently only returning the first instance of the elements. In other words I have 3 or 4 "refdm" elements throughout the document each containing the same set of child elements, but the code only returns the first instance of the "refdm" child elements - not a list of all contained within the document.

Any further pointers would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 03:18 AM

hmm it should extract all elements with that name from your document and store them in a list. can you show us your xml file?
Was This Post Helpful? 0
  • +
  • -

#5 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 03:45 AM

Many thanks again. XML file attached.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 04:34 AM

i try your file and it works for me using MessageBox.Show(xmlNodeList.Count.ToString()); shows that the file have 5 elements with name "refdm". can we see the code that you have

This post has been edited by raziel_: 04 July 2012 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 05:24 AM

Sure. I have replace messagebox with consoleWriteLine but other than that i think the code is the same.

I have just tried it with messageBox and it gives me a seperate messageBox for each reference. Is this what i should be getting? Ideally i'd like a complete list in one box or one console window.

I appreciate the help.

 static void Main(string[] args)
        {
           XmlDocument xmlDoc = new XmlDocument();
	XmlNodeList xmlNodeList;
            try
            {
    xmlDoc.Load("C:\\Documents and Settings\\S0035651\\Desktop\\XML_test.xml"); //Loads the document
	xmlNodeList = xmlDoc.GetElementsByTagName("refdm"); //Select elements using his name
   
                foreach (XmlNode xmlNode in xmlNodeList)
    {
        
        Console.WriteLine("DM Title: " + xmlNode.ChildNodes[0].InnerText); 
        Console.ReadLine();
    }
            }
                catch(Exception e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }



Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 05:44 AM

uhm yes that's what it do. so you get 5 message boxes? well then your rdy :)
using the foreach loop you can manipulate every node with name "refdm".
the ChidNodes is property that returns the nodes that are in your node this means that in your xml when you extract all the "refdm" nodes with ChildNodes you can access the inner nodes.
the InnerText Property shows the text inside the nodes.
hope this makes some sens.

EDIT:

if your "refdm" node looks like this:
<refdm>
<avee>
<modelic>TS</modelic>
<sdc>AAA</sdc>
<chapnum>A00</chapnum>
<section>0</section>
<subsect>0</subsect>
<subject>00</subject> 
<discode>00</discode>
<discodev>00</discodev>
<incode>022</incode>
<incodev>A</incodev>
<itemloc>D</itemloc>
</avee>
</refdm>



then this:

            foreach (System.Xml.XmlNode xmlNode in xmlNodeList)
            {
                
                MessageBox.Show(xmlNode.ChildNodes[0].ChildNodes[0].InnerText);
            }



the first messagebox will show TS. xmlNode contains "refdm" node with all its sub nodes.the first .ChildNodes[0] contains "avee" node and its sub nodes. the second .ChildNodes[0] contains "modelic" node. since he dose not have sub nodes .InnerText shows the text inside the node (TS).

This post has been edited by raziel_: 04 July 2012 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: XmlReader - can't extract the content of child nodes

Posted 04 July 2012 - 05:54 AM

Thank you for your help on this. I think i understand.
Was This Post Helpful? 0
  • +
  • -

#10 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: XmlReader - can't extract the content of child nodes

Posted 28 August 2012 - 03:32 AM

Hi again guys,

With regard to the above solution that you helped me with previously:

If i now wanted to run a similar piece of code to extract text from elements and list them in an output, but get the text from multiple XML source files. How would i achieve this?

Many thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#11 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: XmlReader - can't extract the content of child nodes

Posted 28 August 2012 - 06:22 AM

use a loop to get all files and then just use what you use for one file with every one of them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1