none
DataSet.ReadXML where XML is using Nested Items, how to get only the nested rows RRS feed

  • Question

  • Ok.  So I have  DataSet created using an XML.  The XML is using Nested attributes.  And I just need to know how to loop properly through the DataSet Tables and their columns.

    I have the following XML.

    <?xml version="1.0" encoding="utf-8" ?>
    <Plugin PluginName="TestName" GroupName="Operating System Settings" Type="Single">
      <Title>Plugin 1</Title>
      <StartTime>1:57:47 PM 2/16/2010</StartTime>
      <EndTime>1:58:03 PM 2/16/2010</EndTime>
      <Description>Runs the TestName process to determine something.</Description>
      <Section SectionName="Section1">
        <Field FieldName="Field Name">
          <Value Operand="EQ">Some Correct Setting1</Value>
          <ActionPlugin Name="" Type="Link" URL="">
            <Executable>SomeAction1.exe</Executable>
            <Parameters>Param1 Param2</Parameters>
          </ActionPlugin>
        </Field>
        <Field FieldName="Field2">
          <Value Operand="RG">900</Value>
          <WarningLevel>10%</WarningLevel>
          <ErrorLevel>20%</ErrorLevel>
          <ActionPlugin Name="ActionPlugin1" Type="Link" URL="http://www.somesite.tld/some/path/file.htm" />
        </Field>
      </Section>
      <Section SectionName="Section2">
        <Field FieldName="Field1">
          <Value Operand="EQ">Some Correct Setting2</Value>
          <ActionPlugin Name="" Type="Link" URL="">
            <Executable>SomeAction2.exe</Executable>
            <Parameters>Param1 Param2</Parameters>
          </ActionPlugin>
        </Field>
        <Field FieldName="Field2">
          <Value Operand="RG">900</Value>
          <WarningLevel>10%</WarningLevel>
          <ErrorLevel>20%</ErrorLevel>
          <ActionPlugin Name="ActionPlugin1" Type="Link" URL="http://www.somesite.tld/some/path/file.htm" />
        </Field>
      </Section>
      <Section SectionName="Section3">
        <Field FieldName="Field1">
          <Value Operand="EQ">Some Correct Setting3</Value>
          <ActionPlugin Name="" Type="Link" URL="">
            <Executable>SomeAction3ds.exe</Executable>
            <Parameters>Param1 Param2</Parameters>
          </ActionPlugin>
        </Field>
        <Field FieldName="Field2">
          <Value Operand="RG">900</Value>
          <WarningLevel>10%</WarningLevel>
          <ErrorLevel>20%</ErrorLevel>
          <ActionPlugin Name="ActionPlugin1" Type="Link" URL="http://www.somesite.tld/some/path/file.htm" />
        </Field>
      </Section>
    </Plugin>


    So the DataSet is created with these tables:

    -        List    Count = 5    System.Collections.ArrayList
    +        [0]    {Plugin}    object {System.Data.DataTable}
    +        [1]    {Section}    object {System.Data.DataTable}
    +        [2]    {Field}    object {System.Data.DataTable}
    +        [3]    {Value}    object {System.Data.DataTable}
    +        [4]    {ActionPlugin}    object {System.Data.DataTable}


    Table [2] {Field} has 6 rows.

    So I need to do this:

    string mFullPathToXML = "C:\My.xml";
    DataSet ds;
    ds.ReadXml(mFullPathToXML);
    
    foreach (DataRow SectionRow in ds.Tables["Section"].Rows)
    {
        foreach(DataRow FieldRow in ds.Tables["Field"].Rows) 
        {
            // How to get only the two rows that belong here?
        }
    }
    

    I found a forum post that suggests that I use XmlNodes instead of using a DataSet but this program is already designed using DataSet and so I would prefer to do it with a DataSet.

    It must be possible with a DataSet, otherwise, how could it write the XML back out in the same format.

    Anyway, if anyone knows the answer, that would be awesome.
    Tuesday, February 16, 2010 8:30 PM

Answers

  • Hello Karwinlee,

     

    Welcome to ADO.NET DataSet forum!

     

    If I understand the problem correctly, you can consider using the following sample codes to get the Section table’s related rows in Field table:

    ======================================================================

                foreach (DataRow sectionRow in ds.Tables["Section"].Rows)

                {

                    string sectionId = sectionRow["Section_Id"].ToString();

                    Console.WriteLine("Section: {0}", sectionRow["SectionName"]);

                    foreach (DataRow fieldRow in ds.Tables["Field"].Select("Section_Id = " + sectionId))

                    {

                        foreach (object item in fieldRow.ItemArray)

                        {

                            Console.Write(item.ToString() + "  ");

                        }

                        Console.WriteLine();

                    }

                    Console.WriteLine();

                }

    ======================================================================

    First, we retrieve the primary key column (Section_Id) value of each row in the Section table.  The column is automatically created by the DataSet.   Then in the Field table, we select (DataTable.Select()) all the rows whose Section_Id foreign key column value equals the primary key value of the Section table.    

     

    The output result of above sample codes:

    ======================================================================

    Section: Section1

    0      Field Name  0 

    1  10%  20%  Field2  0 

     

    Section: Section2

    2      Field1  1 

    3  10%  20%  Field2  1 

     

    Section: Section3

    4      Field1  2 

    5  10%  20%  Field2  2 

    ======================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,

    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Karwinlee Wednesday, February 17, 2010 4:22 PM
    Wednesday, February 17, 2010 7:13 AM
    Moderator

All replies

  • Hello Karwinlee,

     

    Welcome to ADO.NET DataSet forum!

     

    If I understand the problem correctly, you can consider using the following sample codes to get the Section table’s related rows in Field table:

    ======================================================================

                foreach (DataRow sectionRow in ds.Tables["Section"].Rows)

                {

                    string sectionId = sectionRow["Section_Id"].ToString();

                    Console.WriteLine("Section: {0}", sectionRow["SectionName"]);

                    foreach (DataRow fieldRow in ds.Tables["Field"].Select("Section_Id = " + sectionId))

                    {

                        foreach (object item in fieldRow.ItemArray)

                        {

                            Console.Write(item.ToString() + "  ");

                        }

                        Console.WriteLine();

                    }

                    Console.WriteLine();

                }

    ======================================================================

    First, we retrieve the primary key column (Section_Id) value of each row in the Section table.  The column is automatically created by the DataSet.   Then in the Field table, we select (DataTable.Select()) all the rows whose Section_Id foreign key column value equals the primary key value of the Section table.    

     

    The output result of above sample codes:

    ======================================================================

    Section: Section1

    0      Field Name  0 

    1  10%  20%  Field2  0 

     

    Section: Section2

    2      Field1  1 

    3  10%  20%  Field2  1 

     

    Section: Section3

    4      Field1  2 

    5  10%  20%  Field2  2 

    ======================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,

    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Karwinlee Wednesday, February 17, 2010 4:22 PM
    Wednesday, February 17, 2010 7:13 AM
    Moderator
  • Thank you.  This worked perfectly.
    http://rhyous.com Never stop acquiring knowledge, as it goes with you when you die.
    Wednesday, February 17, 2010 4:22 PM
  • You are welcome! 

     

    Have a nice day!

     

     

    Best Regards,

    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, February 18, 2010 12:57 AM
    Moderator