none
How to get data out of an XML file in C#

    Pregunta

  • Hi there,

    I'm a newbie to programming with C#, so please help in anyway you can Thanks.

    Using:
    MS Visual C# 2005 express edition on Windows XP.

    What I'm trying to do:
    I would like to extract just a couple of pieces of information from a data file, for example this XML created file:
    =======File starts=======
    <?xml version="1.0"?>
    <!-- created by the MBM 5 logger.dll -->
    <MBM_INTERVAL>
    <Log Date="05/03/2006" Time="18:26:03" CPU="3205 MHz" T_Case="32&#186; C" T_CPU="47&#186; C" T_PSU="-48&#186; C" V_Core_0="1.34 V" V_Core_1="3.14 V" V_3.3="3.22 V" V_5.00="5.05 V" V_12.00="11.97 V" V_Minus_12.00="0.00 V" V_Minus_5.00="0.00 V" F_Fan_1="2616 RPM" F_Fan_2="1962 RPM" F_Fan_3="0 RPM"/>
    </MBM_INTERVAL>
    =======File End======

    I would like to extract the CPU temp (T_CPU) or other temps or bits of data from the log file.

    If it's of help I can also save in these data formats:
    Plain TXT file, CSV, HTML and XML.  Just in case I'm doing it the hard way trying to work with XML data.

    Thanks for any help or advice for a newb.

    domingo, 05 de marzo de 2006 18:40

Respuestas

  • hi,

    this is called xpath you use to search for particular node text or attribute text in your case t_cpu is called attribute you can read attribute like you read childnode in xpath but you start the name with @ so you start it with @T_CPU not like the node name, you can try something like this


    System.Xml.XmlDocument myxmldoc = new System.Xml.XmlDocument();
    myxmldoc.Load(
    "MyXMLFile1.xml");//if you have the xmlfile outside the solution explorer insert the full path to it

    //read single node

    string messg = myxmldoc.SelectSingleNode("MBM_INTERVAL/Log/@T_CPU").InnerText ;
    MessageBox.Show(messg);
    //read a collection of nodes if your file have many <Log> nodes

    System.Xml.XmlNodeList mynodes = myxmldoc.SelectNodes("MBM_INTERVAL/Log/@T_CPU");
    foreach (System.Xml.XmlNode mynode in mynodes)
    {
    MessageBox.Show(mynode.InnerText);
    }

    //remove the xmldocument from memory

    myxmldoc = null;


     

    hope this helps

    domingo, 05 de marzo de 2006 22:23

Todas las respuestas

  • Hi,
    the easiest way I know to do that is to use the XmlTextReader. Check this page for examples: http://www.c-sharpcorner.com/Tutorials/ReadWriteXMLTutMellli21.asp
    domingo, 05 de marzo de 2006 19:01
  • Ok, I've read the info in that link and it's pretty confusing for a newbie.  But still a lot better than any information I've found with the MS searches.

    I've got my Log file size down to just:
    "<?xml version="1.0"?>
    <!-- created by the MBM 5 logger.dll -->
    <MBM_INTERVAL>
    <Log Date="05/03/2006" Time="19:11:49" CPU="3209 " T_Case="33 " T_CPU="50 " T_PSU="-48 "/>
    </MBM_INTERVAL> "

    But as you can see the information is all on one line.  I only need the T_CPU number, which is 50 in this case.  So how would I search/filter this information out of this XML line and file?

    All the example code I've found just displays the whole file.  And doesn't work on this dodgy XML output!  As it only returns:
    <MBM_INTERVAL> <Log> </MBM_INTERVAL>
    Yep not even the data on that line, just the word Log!

    Thanks again for any help or advice, or what to search for to point me in the right direction.

    domingo, 05 de marzo de 2006 19:36
  • hi,

    this is called xpath you use to search for particular node text or attribute text in your case t_cpu is called attribute you can read attribute like you read childnode in xpath but you start the name with @ so you start it with @T_CPU not like the node name, you can try something like this


    System.Xml.XmlDocument myxmldoc = new System.Xml.XmlDocument();
    myxmldoc.Load(
    "MyXMLFile1.xml");//if you have the xmlfile outside the solution explorer insert the full path to it

    //read single node

    string messg = myxmldoc.SelectSingleNode("MBM_INTERVAL/Log/@T_CPU").InnerText ;
    MessageBox.Show(messg);
    //read a collection of nodes if your file have many <Log> nodes

    System.Xml.XmlNodeList mynodes = myxmldoc.SelectNodes("MBM_INTERVAL/Log/@T_CPU");
    foreach (System.Xml.XmlNode mynode in mynodes)
    {
    MessageBox.Show(mynode.InnerText);
    }

    //remove the xmldocument from memory

    myxmldoc = null;


     

    hope this helps

    domingo, 05 de marzo de 2006 22:23
  • Thank you thank you thank you :)  Your code worked brilliantly.
    lunes, 06 de marzo de 2006 16:34
  • you welcome
    lunes, 06 de marzo de 2006 20:05