How to Read XML String into dataset?


I'm grabbing an XML string from a database and trying to pass it into a
dataset. Please correct me if i'm wrong

Private Function GetDataSet1() As DataSet
Dim ds As DataSet = New DataSet

Dim sb As New System.Text.StringBuilder
sb.Append("<?xml version=""1.0""?><items>")
sb.Append("<item>")
sb.Append("<itemid>100</itemid>")
sb.Append("<friendlyname1>Vegetables</friendlyname1>")
sb.Append("<friendlyname2>VG</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>200</itemid>")
sb.Append("<friendlyname1>Fruits</friendlyname1>")
sb.Append("<friendlyname2>FR</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>1001</itemid>")
sb.Append("<friendlyname1>Potatoes</friendlyname1>")
sb.Append("<friendlyname2>PO</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>1002</itemid>")
sb.Append("<friendlyname1>Spinach</friendlyname1>")
sb.Append("<friendlyname2>SP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>2001</itemid>")
sb.Append("<friendlyname1>Apples</friendlyname1>")
sb.Append("<friendlyname2>AP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>2002</itemid>")
sb.Append("<friendlyname1>Bananas</friendlyname1>")
sb.Append("<friendlyname2>BN</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>20001</itemid>")
sb.Append("<friendlyname1>Granny Smith</friendlyname1>")
sb.Append("<friendlyname2>GS</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>20002</itemid>")
sb.Append("<friendlyname1>Macintosh</friendlyname1>")
sb.Append("<friendlyname2>MA</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")
sb.Append("</items>")
Dim ms As New System.IO.MemoryStream
Dim writer As New System.IO.StreamWriter(ms)
writer.Write(sb.ToString())
writer.Flush()
ms.Position = 0
ds.ReadXml(ms)
Return ds
End Function 'GetDataSet1

private static EmpDataDS GetDataSet1()
{
EmpDataDS ds = new EmpDataDS();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><EmpDataDS>");
sb.Append("<Emp>");
sb.Append("<EmpID>123</EmpID>");
sb.Append("<LastName>Smith</LastName>");
sb.Append("<FirstName>John</FirstName>");
sb.Append("<SSN>222-22-2222</SSN>");
sb.Append("<DateOfBirth>" + DateTime.Now.ToLongDateString() +
"</DateOfBirth>");
sb.Append("<Litho1>litho123</Litho1>");
sb.Append("<Litho2>litho1 3</Litho2>");
sb.Append("</Emp>");
sb.Append("<Emp>");
sb.Append("<EmpID>456</EmpID>");
sb.Append("<LastName>Jones</LastName>");
sb.Append("<FirstName>Mary</FirstName>");
sb.Append("<SSN>333-33-3333</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");

for(int i = 0; i < 21 ; i++)
{

sb.Append("<Emp>");
sb.Append("<EmpID>" + (i+1000) + "</EmpID>");
sb.Append("<LastName>"+"EmpLastName"+"</LastName>");

sb.Append("<FirstName>"+"EmpFirstName:"+Convert.To String(i)+"</FirstName>");
sb.Append("<SSN>" + Convert.ToString(( Convert.ToString(i+1000) +
"000000000" )).Substring(0,9) + "</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");
}
sb.Append("</EmpDataDS>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}

No comments:

Post a Comment

Recent Posts