Maybe you can help me, how the heck do I use "smart" fill via XML and DataSet ?
I’m not a happy coder right now.
I gave Eran, a new gifted programmer in my department, a task:
I have a simple table in my database, named Users, with the fields ID (int, identity) and Name(varchar).
I want to send xml to a web-service which looks like this:
<User>
<ID>1</ID>
<Name>Oren</Name>
</User>
I want the web-service to add this row to the Users table in some magic way. I thought that by using Typed DataSet, which will use the table’s schema, and loading the xml into it will do the trick, but I was wrong. We tried many things but they didn’t seem to work.
The reason I want to use schema is to verify the given data(xml) structure before I insert it to the DB (and of course, without it how the DS(DataSet) will “know” to load it properly?). In addition, if tomorrow I need to add Password field to Users table, the only thing I’ll have to do is to re-generate the Typed DataSet and set the password via the xml (<Password>mypassword</Password>).
The code looks like this:
string sXml = “<User><Id>1</Id><Name>Oren</Name></User>”;
using (SqlDataAdapter adapter = new SqlDataAdapter(“”, connString))
{
UsersDS ds = new UsersDS();
System.IO.StringReader xmlSR = new System.IO.StringReader(sXml);
ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
ds.ReadXmlSchema(HttpContext.Current.Server.MapPath (“.”) + “\\UsersDS.xsd”);
adapter.Update(ds.Tables[“Users”]);
}
For some reason my ds isn’t getting filled.
Do you have any idea what am I doing wrong ?
btw – I know I can always parse the xml by myself and create the required commands, but I thought it will be a maintenance nightmare and less comfortable than letting the schema & the Typed DataSet doing the job for me.
Update [20.09.2005]:
Amir Markowitz, again, came to the rescue and with his help we’ve managed to solve this one:
using (SqlDataAdapter adapter = new SqlDataAdapter(“SELECT * FROM Users”, connString))
{
SqlCommandBuilder scb = new SqlCommandBuilder (adapter) ;
UsersDS ds = new UsersDS();
XmlTextReader reader = new XmlTextReader(new StringReader(sXml));
ds.ReadXml(reader);
ds.ReadXmlSchema(HttpContext.Current.Server.MapPath (“.”) + “\\UsersDS.xsd”);
adapter.Update(ds.Tables[“Users”]);
}
Now, the xml data (in “sXml” variable) looks like this:
<UsersDS xmlns=“http://tempuri.org/UsersDS.xsd”>
<Users>
<UserName>Orenn</UserName>
<Name>Oren E.</Name>
</Users>
</UsersDS>
So thanks again Markowitz, I owe you one (again, add it to my list).