473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

NewRow fails when using XML document as Dataset

Hi All,

I am using following code snippet to add another user in Users.xml:
-------------------------------------------------------------------
DataSet dstUsers = new DataSet();
dstUsers.ReadXml("Users.xml");
DataTable dtbUsers = dstUsers.Tables["User"];
DataRow drwUser = dtbUsers.NewRow();
drwUser["UserName"] = "Test User 2";
dtbUsers.Rows.Add(drwUser);
dstUsers.WriteXml("Users.xml");
-------------------------------------------------------------------
Users.xml looks like
-------------------------------------------------------------------
<?xml version="1.0" standalone="yes"?>
<Configuration>
<Users>
<User>
<UserName>Test User</UserName>
</User>
</Users>
<Roles>
<Role>
<RoleName>Test Role</RoleName>
</Role>
</Roles>
</Configuration>
-------------------------------------------------------------------

After execution of code, <User> tag is inserted at wrong position and
Users.xml looks like:
-------------------------------------------------------------------
<?xml version="1.0" standalone="yes"?>
<Configuration>
<Users>
<User>
<UserName>Test User</UserName>
</User>
</Users>
<User>
<UserName>Test User 2</UserName>
</User>
<Roles>
<Role>
<RoleName>Test Role</RoleName>
</Role>
</Roles>
</Configuration>
-------------------------------------------------------------------

But if I modify Users.xml as below, <User> tag is inserted at proper
position.
-------------------------------------------------------------------
<?xml version="1.0" standalone="yes"?>
<Users>
<User>
<UserName>Test User</UserName>
</User>
</Users>
-------------------------------------------------------------------

Can anyone give any idea that how can I insert <User> tag inside
<Users> tag without modifying XML format like above???
Nov 12 '05 #1
1 2547
"Piyush Gupta" <pi*******@gmail.com> wrote in message news:f6**************************@posting.google.c om...
Can anyone give any idea that how can I insert <User> tag inside
<Users> tag without modifying XML format like above???
What you probably think you're seeing when you look at
the XML fragment,

: : <Users>
<User>
<UserName>Test User</UserName>
</User>
</Users>
is one DataTable named "User," based on the simple
statements you've used to add a row to this table,

: : DataTable dtbUsers = dstUsers.Tables["User"];
DataRow drwUser = dtbUsers.NewRow();
drwUser["UserName"] = "Test User 2";
dtbUsers.Rows.Add(drwUser);
but this appearance can, perhaps, be misleading.

Actually, there are 2 DataTables in the above XML
fragment, and no less than 3 DataColumns (not one).
If you read the DataRow in dstUsers.Tables["User"]
to begin with, you'll see two of these DataColumns,
the "UserName" column and the "Users_Id" 'foreign
key' column that relates the "User" child DataTable
to it's "Users" parent DataTable.

: : <Users>
<User>
<UserName>Test User</UserName>
</User>
</Users>
<User>
<UserName>Test User 2</UserName>
</User>


The <User> record here appears outside of a <Users>
container because when adding a new DataRow you've
specified no value for the Users_Id column that
tells the DataSet which <Users> container element
that <User> record belongs to. Since it's value
for Users_Id is 'null', it appears outside of any
containing <Users> element.

The corrected code would look like this,

DataTable dtbUsers = dstUsers.Tables[ "User"];
DataRow drwUser = dtbUsers.NewRow( );
drwUser[ "UserName"] = "Test User 2";
drwUser[ "Users_Id"] = 0; // Put under first <Users> element.
dtbUsers.Rows.Add( drwUser);

If the XML document you're working with isn't truly a
relational data set, then using DataSet to manipulate
that document should be discouraged. You'll have a
much easier time working directly with an XmlDocument,
instead:

// Create and load XmlDocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load( "Users.xml");

// Position yourself where you want to insert new elements.
XmlNode eUsers = xmlDoc.DocumentElement.FirstChild;

// Create <User>/<UserName>/#text.
XmlElement eUserNew = xmlDoc.CreateElement( "User");
XmlElement eUserNameNew = xmlDoc.CreateElement( "UserName");
XmlText txUserNameNew = xmlDoc.CreateTextNode( "Test User 2");

// Connect the XmlNodes you've just created.
eUserNameNew.AppendChild( txUserNameNew);
eUserNew.AppendChild( eUserNameNew);
eUsers.AppendChild( eUserNew);

The XmlDocument will hold an open file handle when you call
Load() with the filename string, so it's usually best to
cons'up an XmlTextReader to load the file (then close the
XmlTextReader after the load operation has completed) and
use an XmlTextWriter to save the file.

However, you never have to guess what your data model is,
as when trying to shove these manipulations through the
DataSet API (which is really intended for other purposes.)
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: CB | last post by:
Using C# in .Net 2003, DataSet.ReadXml fails when a percentage (%) sign is in the filename followed by 2 hex characters. Seems that the % sign is likely encoding the following 2 hex characters. ...
0
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
14
by: Agnes | last post by:
I only one datagrid, and one button. the buttonwill process ' dtmyTable.newrow()' , However, the datagrid seems didn't add the row byitself ?? Thanks
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
1
by: mrstrong | last post by:
Gday, I have just started c# .net about 3 months ago and have recently (last week) upgraded to 2005 and hoping for some assistance understanding the best approach to adding new records to a...
0
by: c.w.browne | last post by:
Hi, Ive had a bit of a look around for other people with this problem and cant find anything that solves it in my case, so I'm afraid im going to have to bother you all with a post of my own. ...
2
by: ERNESTP | last post by:
Hi all, I am starting to learn how to program VB.Net and I found got an problem when try to insert a new row into the table. I had create an form to whole all the DataSet, OleDbDataAdaper and...
1
by: Pitmaster | last post by:
Hi there, hoping somebody can help out here. Searched half earth to find a solution but no success yet. The following code doesn't gave any fault or warning but doesn't what it is suppose to do....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.