Problem with reading .xml file using Dataset method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raghudr@gmail.com

    #1

    Problem with reading .xml file using Dataset method

    Hi all,

    I am parsing an .xml file.My main intention is to retrieve the field
    value:- "Name Value" which is "rag" and store

    it in a List.

    Fot that i wrote code like this:

    //i am using dataset method to read the .xml files
    DataSet pcXML = new DataSet("pcXML" );

    //
    try
    {

    pcXML.ReadXml(" filename.xml", XmlReadMode.Aut o);

    }
    catch (Exception ex)
    {
    MessageBox.Show (ex.Message,"Er ror", MessageBoxButto ns.OK);
    return false;

    }

    //storing the names only to list

    List<stringname s = new List<string>();

    // Add the extracted variables to the list
    foreach (DataRow pcRow in pcXML.Tables["Name"].Rows)
    {
    names.Add(pcRow[0].ToString());
    }

    The .xml file is given below
    <Signal>
    <Name Value="rag" />
    <Real Value="21@ADVAN T" />
    <Description Value="Best" />
    <Min Value="0" />
    <Max Value="40" />
    <Unit Value="barh" />
    <Item Value="XQ60" />
    <MillisCylce Value="20" />
    <Resolution Value="1" />
    <DataType Value="VT" />
    </Signal>
    <Signal>
    <Name Value="rock" />
    <Real Value="21@ADVAN T" />
    <Description Value="Best" />
    <Min Value="0" />
    <Max Value="40" />
    <Unit Value="barh" />
    <Item Value="XQ60" />
    <MillisCylce Value="20" />
    <Resolution Value="1" />
    <DataType Value="VT" />
    </Signal>

    //The above code works fine if the .xml file is of the above format.I
    am able to retrieve all the names and store in

    the list.

    ------------------------------------------------------------------------------
    Problem:

    Now some .xml files are this format:

    <Signal>
    Name Value="rag" />
    <Real Value="21@ADVAN T" />
    <Description Value="Best" />
    <Min Value="0" />
    <Max Value="40" />
    <Unit Value="barh" />
    <Item Value="XQ60" />
    <MillisCylce Value="20" />
    <Resolution Value="1" />
    <DataType Value="VT" />
    <SubSignals>
    <Signal BitIndex="0">
    <Name Value="jack" />
    <Reference Value="" />
    <Description Value="AIR_PRG" />
    <Min Value="11" />
    <Max Value="13" />
    <Unit Value="ACTIVE" />
    <DataType Value="VT" />
    </Signal>
    <Signal BitIndex="1">
    <Name Value="jae" />
    <Reference Value="" />
    <Description Value="AIR_PRG" />
    <Min Value="11" />
    <Max Value="13" />
    <Unit Value="ACTIVE" />
    <DataType Value="VT" />
    </Signal>
    <Signal BitIndex="2">
    <Name Value="chuck" />
    <Reference Value="" />
    <Description Value="AIR_PRG" />
    <Min Value="11" />
    <Max Value="13" />
    <Unit Value="ACTIVE" />
    <DataType Value="VT" />
    </Signal>
    </SubSignals>
    </Signal>

    Problem is :

    In the above .xml file format with my code when i try to read the .xml
    file i am getting an exception "The

    Table(signal) cannot be the child table to itself in nested
    relations".

    Here also i want to store only the "<signalnam e value" that is only
    "rag" to my list.
    I do not want to add any "subsignals " name value to the list.i want to
    skip it.

    can anyone tell me what changes i need to do to my above code.

    thanks in advance,
    RAGHU
  • Martin Honnen

    #2
    Re: Problem with reading .xml file using Dataset method

    raghudr@gmail.c om wrote:
    can anyone tell me what changes i need to do to my above code.
    I don't think using a DataSet with that kind of XML structure is going
    to work. Use XmlReader or XPathDocument/XPathNavigator or with .NET
    3.5 LINQ to XML to parse that XML, those APIs can deal with any
    well-formed XML document (respectively in the case of XmlReader and
    XPathDocument even with any kind of well-formed fragment).


    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    Working...