Apologies in advance
I'm new to this and trying to learn as I go along. I have created a website that allows a user to view production data and I would like to know is it possible to allow the website admin to import data via xml document into the already created database table,
there are only InvoiceDate, AmountInLitres, PricePerLitre, FatContent, LactoseContent, AntiBodyCount, HerdNumber,MilkId fields in the table. I would like to do this by the click of a button or something like it. I have looked at some of the resources to try
and get grasp on what to do but as I'm a completely new to xml I don't know where to start.
is it possible to allow the website admin to import data via xml document into the already created database table,
In fact you can use create a memory-based DataTable first to inserting, deleting or updating things of data contents,and then use ReadXml/WriteToXml to R/W……
Thanks for the replies, Are there any good walk throughs or tutorials for beginners that you could recommend. I have until next Tuesday 24th April to have this functioning so there is a slight panic on
Are there any other tutorials, I'm not using Silverlight, I'm using V S 2010 ultimate building a basic website. I think the tutorial needs to be for dummies type of a guide before I'll be able to understand this. I've been searching the web but cant find
a XML to database read (from the XML file) and write( to the database) process. I understand the XML file and writing it will not be a problem its the process of reading in the data and placing it into the already connected database and time unfortunately
is in short supply and the panic is on.
Just like my ideas to use DataSet's ReadXml,sample from MSDN:
private void DemonstrateReadWriteXMLDocumentWithStreamReader()
{
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("dataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
OriginalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the schema and data to an XML file.
string xmlFilename = "XmlDocument.xml";
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlFilename);
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document into the DataSet.
newDataSet.ReadXml(xmlFilename);
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\table " + row[column] );
}
Console.WriteLine();
}
}
}
DataSet's WriteXml——Just add a DataTable into the DataSet and then use DataSet.WriteXml("xxx.xml");
Thanks Decker for you reply, I have taken the bull by the horns and have tried to do this using your help and some books and whats left of my brain. So far I have added a button to the admin page and in the code behind (this is where it starts to get shakey)
I have placed the code that I have come up with to read an XML file from a fixed location(My desktop). I'm not sure if this is the proper procedure but it looked OK to me but it dosen't work surprise,surprise. I wanted to give the admin a way of importing
the XML directly into the database table by clicking a button. I have used some of Murachs 2008 code and butchered it to try and get it to work with my database table(MilkDetails) but VS highlights MilkDetails and throws an error"The
type or namespace name 'MilkDetails' could not be found (are you missing a using directive or an assembly reference?)" but intelisense offered the rest of the tables fields so I'm confused and it dosen't like the date either and has highlighted .."xmlIn.ReadElementContentAsDateTime("InvoiceDate");"
I have included the XML after the .cs file it only one row to show what the data looks like
There are a fewQuestions
1. Am I on the right track using the code behind the button for this action?
2. Is my code for the actual XML right and are there any other gapping holes ?
3. If I do get it to read the XML file how do I get it into the database table "MilkDetails"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Web.Security;
using System.Web.UI.DataVisualization.Charting;
public partial class Adminpages_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Create the list
List<MilkDetails> MilkDetail = new List<MilkDetails>();
//Create a path varible
string path = @"C:\Users\Paul\Desktop\XMLFile.xml";
//create xml reader settings object
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
//create XML reader object
XmlReader xmlIn = XmlReader.Create(path, settings);
//read past all nodes to the first MilkDetails node
if (xmlIn.ReadToDescendant("MilkDetails"))
{
do// create one MilkDetails object for each MilkDetails node
{
MilkDetail milk = new MilkDetail();
milk.InvoiceDate = xmlIn.ReadElementContentAsDateTime("InvoiceDate");
xmlIn.ReadStartElement("InvoiceDate");
milk.AmountInLitres = xmlIn.ReadElementContentAsString();
milk.PricePerLitre = xmlIn.ReadElementContentAsDecimal();
milk.FatContent = xmlIn.ReadElementContentAsString();
milk.LactoseContent = xmlIn.ReadElementContentAsString();
milk.AntiBodyCount = xmlIn.ReadElementContentAsString();
milk.HerdNumber = xmlIn.ReadElementContentAsString();
MilkDetail.Add(MilkDetails);
}
while(xmlIn.ReadToNextSibling("MilkDetails"));
}
//close the xml reader
xmlIn.Close();
}
}
The type or namespace name 'MilkDetails' could not be found (are you missing a using directive or an assembly reference?)
From your codes I guess that your MilkDetails is a class of entity or something like that,So plz make sure that it really defined or you've referred it into your own proj by referring its namespace at the top of your current codes。I suspect that
you should change from "MilkDetails" to "MilkDetail"——
List<MilkDetail> MilkDetail = new List<MilkDetail>();
From your given xml contents,I'd prefer to read it like using LINQ-TO-XML(suppose you have a class called "MilkDetail" with the public properties whose names are the same as what they are defined in the xml file……):
var result = from e in XDocument.Load("xx.xml").Descedants("MilkDetails")
select new MilkDetail
{
InvoiceDate = e.Element("InvoiceDate").Value,
AmountInLitres = e.Element("AmountInLitres").Value,
……………………
};
Cheiftain
Member
33 Points
40 Posts
Is this possible
Apr 14, 2012 11:18 AM|LINK
Hi Guys
Apologies in advance I'm new to this and trying to learn as I go along. I have created a website that allows a user to view production data and I would like to know is it possible to allow the website admin to import data via xml document into the already created database table, there are only InvoiceDate, AmountInLitres, PricePerLitre, FatContent, LactoseContent, AntiBodyCount, HerdNumber,MilkId fields in the table. I would like to do this by the click of a button or something like it. I have looked at some of the resources to try and get grasp on what to do but as I'm a completely new to xml I don't know where to start.
mm10
Contributor
6445 Points
1187 Posts
Re: Is this possible
Apr 14, 2012 12:50 PM|LINK
Yes, you can for example use an XmlReader to read the contents of the document. Read more here: http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx#Y610
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Is this possible
Apr 16, 2012 01:50 AM|LINK
In fact you can use create a memory-based DataTable first to inserting, deleting or updating things of data contents,and then use ReadXml/WriteToXml to R/W……
Reguards!
Cheiftain
Member
33 Points
40 Posts
Re: Is this possible
Apr 16, 2012 01:17 PM|LINK
Thanks for the replies, Are there any good walk throughs or tutorials for beginners that you could recommend. I have until next Tuesday 24th April to have this functioning so there is a slight panic on
Cheers
mm10
Contributor
6445 Points
1187 Posts
Re: Is this possible
Apr 16, 2012 01:26 PM|LINK
How To: Parse XML with XmlReader (http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx#Y572)
Cheiftain
Member
33 Points
40 Posts
Re: Is this possible
Apr 16, 2012 08:24 PM|LINK
Thanks again for replying
Are there any other tutorials, I'm not using Silverlight, I'm using V S 2010 ultimate building a basic website. I think the tutorial needs to be for dummies type of a guide before I'll be able to understand this. I've been searching the web but cant find a XML to database read (from the XML file) and write( to the database) process. I understand the XML file and writing it will not be a problem its the process of reading in the data and placing it into the already connected database and time unfortunately is in short supply and the panic is on.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Is this possible
Apr 17, 2012 01:13 AM|LINK
Hello:)
Just like my ideas to use DataSet's ReadXml,sample from MSDN:
private void DemonstrateReadWriteXMLDocumentWithStreamReader() { // Create a DataSet with one table and two columns. DataSet OriginalDataSet = new DataSet("dataSet"); OriginalDataSet.Namespace= "NetFrameWork"; DataTable table = new DataTable("table"); DataColumn idColumn = new DataColumn("id", Type.GetType("System.Int32")); idColumn.AutoIncrement= true; DataColumn itemColumn = new DataColumn("item"); table.Columns.Add(idColumn); table.Columns.Add(itemColumn); OriginalDataSet.Tables.Add(table); // Add ten rows. DataRow newRow; for(int i = 0; i < 10; i++) { newRow = table.NewRow(); newRow["item"]= "item " + i; table.Rows.Add(newRow); } OriginalDataSet.AcceptChanges(); // Print out values of each table in the DataSet // using the function defined below. PrintValues(OriginalDataSet, "Original DataSet"); // Write the schema and data to an XML file. string xmlFilename = "XmlDocument.xml"; // Use WriteXml to write the document. OriginalDataSet.WriteXml(xmlFilename); // Dispose of the original DataSet. OriginalDataSet.Dispose(); // Create a new DataSet. DataSet newDataSet = new DataSet("New DataSet"); // Read the XML document into the DataSet. newDataSet.ReadXml(xmlFilename); // Print out values of each table in the DataSet // using the function defined below. PrintValues(newDataSet,"New DataSet"); } private void PrintValues(DataSet dataSet, string label) { Console.WriteLine("\n" + label); foreach(DataTable table in dataSet.Tables) { Console.WriteLine("TableName: " + table.TableName); foreach(DataRow row in table.Rows) { foreach(DataColumn column in table.Columns) { Console.Write("\table " + row[column] ); } Console.WriteLine(); } } }DataSet's WriteXml——Just add a DataTable into the DataSet and then use DataSet.WriteXml("xxx.xml");
Reguards!
Cheiftain
Member
33 Points
40 Posts
Re: Is this possible
Apr 17, 2012 08:52 PM|LINK
Thanks Decker for you reply, I have taken the bull by the horns and have tried to do this using your help and some books and whats left of my brain. So far I have added a button to the admin page and in the code behind (this is where it starts to get shakey) I have placed the code that I have come up with to read an XML file from a fixed location(My desktop). I'm not sure if this is the proper procedure but it looked OK to me but it dosen't work surprise,surprise. I wanted to give the admin a way of importing the XML directly into the database table by clicking a button. I have used some of Murachs 2008 code and butchered it to try and get it to work with my database table(MilkDetails) but VS highlights MilkDetails and throws an error"The type or namespace name 'MilkDetails' could not be found (are you missing a using directive or an assembly reference?)" but intelisense offered the rest of the tables fields so I'm confused and it dosen't like the date either and has highlighted .."xmlIn.ReadElementContentAsDateTime("InvoiceDate");"
I have included the XML after the .cs file it only one row to show what the data looks like
There are a fewQuestions
1. Am I on the right track using the code behind the button for this action?
2. Is my code for the actual XML right and are there any other gapping holes ?
3. If I do get it to read the XML file how do I get it into the database table "MilkDetails"?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Data; using System.Web.Security; using System.Web.UI.DataVisualization.Charting; public partial class Adminpages_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //Create the list List<MilkDetails> MilkDetail = new List<MilkDetails>(); //Create a path varible string path = @"C:\Users\Paul\Desktop\XMLFile.xml"; //create xml reader settings object XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; //create XML reader object XmlReader xmlIn = XmlReader.Create(path, settings); //read past all nodes to the first MilkDetails node if (xmlIn.ReadToDescendant("MilkDetails")) { do// create one MilkDetails object for each MilkDetails node { MilkDetail milk = new MilkDetail(); milk.InvoiceDate = xmlIn.ReadElementContentAsDateTime("InvoiceDate"); xmlIn.ReadStartElement("InvoiceDate"); milk.AmountInLitres = xmlIn.ReadElementContentAsString(); milk.PricePerLitre = xmlIn.ReadElementContentAsDecimal(); milk.FatContent = xmlIn.ReadElementContentAsString(); milk.LactoseContent = xmlIn.ReadElementContentAsString(); milk.AntiBodyCount = xmlIn.ReadElementContentAsString(); milk.HerdNumber = xmlIn.ReadElementContentAsString(); MilkDetail.Add(MilkDetails); } while(xmlIn.ReadToNextSibling("MilkDetails")); } //close the xml reader xmlIn.Close(); } }///////////////////////////////////////////////////////
XML File
<?xml version="1.0" encoding="utf-8" ?>
<MilkDetails>
<InvoiceDate>01/04/2009</InvoiceDate>
<AmountInLitres>25000</AmountInLitres>
<PricePerLitre>0.275</PricePerLitre>
<FatContent>2.50</FatContent>
<LactoseContent>3.15</LactoseContent>
<AntiBodyCount>550</AntiBodyCount>
<HerdNumber>IE789456123</HerdNumber>
<MilkId>114</MilkId>
</MilkDetails>
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Is this possible
Apr 18, 2012 01:04 AM|LINK
Hello Cheiftain:)
From your codes I guess that your MilkDetails is a class of entity or something like that,So plz make sure that it really defined or you've referred it into your own proj by referring its namespace at the top of your current codes。I suspect that you should change from "MilkDetails" to "MilkDetail"——
From your given xml contents,I'd prefer to read it like using LINQ-TO-XML(suppose you have a class called "MilkDetail" with the public properties whose names are the same as what they are defined in the xml file……):
var result = from e in XDocument.Load("xx.xml").Descedants("MilkDetails") select new MilkDetail { InvoiceDate = e.Element("InvoiceDate").Value, AmountInLitres = e.Element("AmountInLitres").Value, …………………… };Cheiftain
Member
33 Points
40 Posts
Re: Is this possible
Apr 19, 2012 02:01 PM|LINK
Sorry I have had to abandon this, I just can't get it to work or get my head around it and time is running out. Thanks for your help