none
WriteXml: how to write in embedded/cascaded style RRS feed

  • Question

  • Hi,

    I have a DataSet populated with two DataTables: "Father" and "Son". Foreign key constraint from "Son" to "Father" is properly setup.

    When I say DataSet.WriteXml, it first dump all "Father" elements, then dump "Son" elements. What I wanted it to do, is to embed the sons inside the fathers.

    Current behaviour:

    <Father>
     <Name>Father 1</Name> <!--PK-->
    </Father>
    <Father>
     <Name>Father 2</Name>
    </Father>
    
    <Son>
     <FatherName>Father 1</FatherName><!-- FK -->
     <Name>Son 1-1</Name>
    </Son>
    <Son>
     <FatherName>Father 1</FatherName>
     <Name>Son 1-2</Name>
    </Son>
    <Son>
     <FatherName>Father 2</FatherName>
     <Name>Son 2-1</Name>
    </Son>
    <Son>
     <FatherName>Father 2</FatherName>
     <Name>Son 2-2</Name>
    </Son>
    
    

    What I wanted:

    <Father>
     <Name>Father 1</Name> <!--PK-->
     <Sons>
      <Son>
       <FatherName>Father 1</FatherName><!-- FK -->
       <Name>Son 1-1</Name>
      </Son>
      <Son>
       <FatherName>Father 1</FatherName>
       <Name>Son 1-2</Name>
     </Son>
     </Sons>
    </Father>
    
    <Father>
     <Name>Father 2</Name>
     <Sons>
      <Son>
       <FatherName>Father 2</FatherName>
       <Name>Son 2-1</Name>
      </Son>
      <Son>
       <FatherName>Father 2</FatherName>
       <Name>Son 2-2</Name>
      </Son>
     </Sons>
    </Father>
    
    
    
    

    Is there a way to do this?

    Thanks!

     

    Wednesday, July 13, 2011 10:39 PM

Answers

  • Hi,

    Here is a fragment of code, which should work:

    // Get the primary key column from the master table (Father)
    DataColumn primaryKey = myDS.Tables["Father"].Columns["FatherID"];
    
    // Get the foreign key column from the detail table (Son)
    DataColumn foreignKey = myDS.Tables["Son"].Columns["FatherID"];
     
    // Create a relation
    DataRelation relation = myDS.Relations.Add(primaryKey, foreignKey);
    
    // Set the relation to be nested
    relation.Nested = true;
    
    // Write the XML
    myDS.WriteXml("FathersAndSons.xml"), XmlWriteMode.IgnoreSchema);
    
    

    You get the idea - you must create a DataRelation and set it to Nested.

    Best regards,
    Veselin

     


    Veselin Georgiev
    MCT, MCPD
    Blog: My blog (coming coon)
    ---
    Please "Mark As Answer" if I have answered your question. Please "Vote As Helpful" to help other users find a solution quicker and easier. Thank you!
    • Proposed as answer by Veselin.Georgiev Thursday, July 14, 2011 1:45 PM
    • Marked as answer by Silan Liu Thursday, July 14, 2011 9:47 PM
    Thursday, July 14, 2011 1:37 PM

All replies

  • Hi,

    Here is a fragment of code, which should work:

    // Get the primary key column from the master table (Father)
    DataColumn primaryKey = myDS.Tables["Father"].Columns["FatherID"];
    
    // Get the foreign key column from the detail table (Son)
    DataColumn foreignKey = myDS.Tables["Son"].Columns["FatherID"];
     
    // Create a relation
    DataRelation relation = myDS.Relations.Add(primaryKey, foreignKey);
    
    // Set the relation to be nested
    relation.Nested = true;
    
    // Write the XML
    myDS.WriteXml("FathersAndSons.xml"), XmlWriteMode.IgnoreSchema);
    
    

    You get the idea - you must create a DataRelation and set it to Nested.

    Best regards,
    Veselin

     


    Veselin Georgiev
    MCT, MCPD
    Blog: My blog (coming coon)
    ---
    Please "Mark As Answer" if I have answered your question. Please "Vote As Helpful" to help other users find a solution quicker and easier. Thank you!
    • Proposed as answer by Veselin.Georgiev Thursday, July 14, 2011 1:45 PM
    • Marked as answer by Silan Liu Thursday, July 14, 2011 9:47 PM
    Thursday, July 14, 2011 1:37 PM
  • Thanks a lot Vaselin for the quick answer!
    Thursday, July 14, 2011 9:47 PM