Inserting a new row into a dataset table

G

Guest

If I have this as my XML file

<?xml version="1.0" standalone="yes" ?>
- <opml>
- <body>
- <outline text="RssImporter OPML">
<outline title="CNN" htmlUrl=""
xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
</outline>
</body>
</opml>

And I use this code to add a new row

System.Data.DataSet myds = new System.Data.DataSet();
myds.ReadXml(@"C:\Program Files\RSSImporter\Data\feeds.xml");
System.Data.DataRow dr;
dr = myds.Tables[2].NewRow();
dr[0] = "test";
myds.Tables[2].Rows.Add(dr);
myds.AcceptChanges();
myds.WriteXml(@"C:\Program Files\RSSImporter\Data\feeds2.xml");

I end up with:

<?xml version="1.0" standalone="yes"?>
<opml>
<body>
<outline text="RssImporter OPML">
<outline title="CNN" htmlUrl=""
xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
</outline>
</body>
<outline title="test" />
</opml>

But the new row with the title "test" should go right after the CNN entry
like this:

<?xml version="1.0" standalone="yes"?>
<opml>
<body>
<outline text="RssImporter OPML">
<outline title="CNN" htmlUrl=""
xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
<outline title="test" />
</outline>
</body>
</opml>


Any idea on what I could be doing wrong?

Thanks
 
M

Michael Nemtsev

Hello Cooper,

Because this is behavior of DataTable. What are you going to do is to apply
your own XML structure to the DataTable XML format.
It's not good idea, because DataTable Xml structure don't guarantee required
behavior in your case and could be changed any time.

What do you really need is use XSLT to transform your data from the dataTable
XML to your OMPL format (btw using DataTable is not necessary, u even could
use DataReader but more work required for transformation)

C> If I have this as my XML file
C>
C> <?xml version="1.0" standalone="yes" ?>
C> - <opml>
C> - <body>
C> - <outline text="RssImporter OPML">
C> <outline title="CNN" htmlUrl=""
C> xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
C> </outline>
C> </body>
C> </opml>
C> And I use this code to add a new row
C>
C> System.Data.DataSet myds = new System.Data.DataSet();
C> myds.ReadXml(@"C:\Program Files\RSSImporter\Data\feeds.xml");
C> System.Data.DataRow dr;
C> dr = myds.Tables[2].NewRow();
C> dr[0] = "test";
C> myds.Tables[2].Rows.Add(dr);
C> myds.AcceptChanges();
C> myds.WriteXml(@"C:\Program Files\RSSImporter\Data\feeds2.xml");
C> I end up with:
C>
C> <?xml version="1.0" standalone="yes"?>
C> <opml>
C> <body>
C> <outline text="RssImporter OPML">
C> <outline title="CNN" htmlUrl=""
C> xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
C> </outline>
C> </body>
C> <outline title="test" />
C> </opml>
C> But the new row with the title "test" should go right after the CNN
C> entry like this:
C>
C> <?xml version="1.0" standalone="yes"?>
C> <opml>
C> <body>
C> <outline text="RssImporter OPML">
C> <outline title="CNN" htmlUrl=""
C> xmlUrl="http://rss.cnn.com/rss/cnn_topstories.rss" />
C> <outline title="test" />
C> </outline>
C> </body>
C> </opml>
C> Any idea on what I could be doing wrong?
C>
C> Thanks
C>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top