none
Taking large number of records and creating multiple xml files RRS feed

  • Question

  • Let us say that I call a Stored Procedure via SqlCommand in a Console Application.  The Stored Procedure returns data to my console application, and I discover that the Stored Procedure has returned 200 rows.  Using the Linq to XML, and using the XDocument class, how can take the 200 rows that are returned, and create 2 xml files?  The console application that I am developing currently has a requirement where there can be 100 child elements max.  As an example, taken from MSDN sample xml file (books.xml)

    <catalog><bookid="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book><bookid="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description></book><bookid="bk103"><author>Corets, Eva</author><title>Maeve Ascendant</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-11-17</publish_date><description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description></book><bookid="bk104"><author>Corets, Eva</author><title>Oberon's Legacy</title><genre>Fantasy</genre><price>5.95</price><publish_date>2001-03-10</publish_date><description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description></book><bookid="bk105"><author>Corets, Eva</author><title>The Sundered Grail</title><genre>Fantasy</genre><price>5.95</price><publish_date>2001-09-10</publish_date><description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description></book><bookid="bk106"><author>Randall, Cynthia</author><title>Lover Birds</title><genre>Romance</genre><price>4.95</price><publish_date>2000-09-02</publish_date><description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description></book><bookid="bk107"><author>Thurman, Paula</author><title>Splish Splash</title><genre>Romance</genre><price>4.95</price><publish_date>2000-11-02</publish_date><description>A deep sea diver finds true love twenty thousand leagues beneath the sea.</description></book><bookid="bk108"><author>Knorr, Stefan</author><title>Creepy Crawlies</title><genre>Horror</genre><price>4.95</price><publish_date>2000-12-06</publish_date><description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description></book><bookid="bk109"><author>Kress, Peter</author><title>Paradox Lost</title><genre>Science Fiction</genre><price>6.95</price><publish_date>2000-11-02</publish_date><description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description></book><bookid="bk110"><author>O'Brien, Tim</author><title>Microsoft .NET: The Programming Bible</title><genre>Computer</genre><price>36.95</price><publish_date>2000-12-09</publish_date><description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description></book><bookid="bk111"><author>O'Brien, Tim</author><title>MSXML3: A Comprehensive Guide</title><genre>Computer</genre><price>36.95</price><publish_date>2000-12-01</publish_date><description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description></book><bookid="bk112"><author>Galos, Mike</author><title>Visual Studio 7: A Comprehensive Guide</title><genre>Computer</genre><price>49.95</price><publish_date>2001-04-16</publish_date><description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description></book></catalog>

    Keeping this in mind, let's say that my Stored Procedure return 200 rows. Now each row would have the attributes

    that are outlined in the xml above. I need to save two files i.e. (catalog1.xml, and catalog2.xml). Therefore, if I have 400 rows returned, I would have four files.

    Any assistance is greatly appreciated!

    Pranav


    Tuesday, November 28, 2017 5:05 PM

All replies

  • Check out the following. Focus on the query and how I save it to a file.

    https://code.msdn.microsoft.com/Working-with-CheckedListBox-3b765442?redir=0


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 28, 2017 5:30 PM
    Moderator
  • Hi Karen, 

    Thanks for the tip, however, I am not sure I can really understand what is going on. Does this code you higlight enable me to create multiple xml files?  Currently from an initial read, I can only assume that the code creates one XML file.  I can create one XML file already.  I need to know how to create multiple XML files using the XDocument class.  So let us envision I have a List<Car> myCars.  The Car class has the following properties: Make, Model, Year, Color.  Now myCars has 20 records.  How can I create two xml files, where each XML file would result in the following

    Cars1.xml

    <MyCars>

    <!--The first set of 10 cars from the myCars list-->

    </MyCars>

    Cars2.xml

    <MyCars>

    <!--The second set of 10 cars from the myCars list-->

    </MyCars>

    Tuesday, November 28, 2017 5:47 PM
  • Here is what I was thinking, you should be able to get the results of each SELECT statement in your console app so that using the C# code I provided would permit saving two files.

    Unless I'm missing something I saw no reason to use XDocument or classes as I took a different route, obtain results from queries in tangent with xmlReader and xmlDocument to write to physical files. In short if you can produce the proper SQL SELECT I see no reason why 'NOT XML' would not give you the results you are after. Anytime we can do more work server side I roll with this.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 28, 2017 6:13 PM
    Moderator