如何把一个字符串形式的xml文档放入一个dataset中?

boyyao 2003-07-21 03:56:57
string ss = "<?xml version=\"1.0\" standalone=\"yes\"?>";
ss +=@"
<PhoneBooks>
<PhoneBook>
<No>1</No>
<Name>Bob James</Name>
<Phone>99999999</Phone>
</PhoneBook>
<PhoneBook>
<No>2</No>
<Name>Nathan East</Name>
<Phone>23232343</Phone>
</PhoneBook>
<PhoneBook>
<No>3</No>
<Name>Dave Koz</Name>
<Phone>24343434</Phone>
</PhoneBook>
<PhoneBook>
<No>4</No>
<Name>Lee Ritenour</Name>
<Phone>343443343</Phone>
</PhoneBook>
<PhoneBook>
<No>5</No>
<Name>濮氱懚</Name>
<Phone>13817918564</Phone>
</PhoneBook>
</PhoneBooks>";

比如。以上一个xml文档。如何把这个ss字符传变成一个dataset
请填写以下一个函数
protected DataSet LoadMyPBData(string 算算)
{
return dataSet;
}
...全文
33 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dreammaster 2003-07-22
  • 打赏
  • 举报
回复
StringReader reader = new StringReader(yourxmlstring);

DataSet1.ReadXml(reader);

ReadXml(string)--string是指文件名。
雪狼1234567 2003-07-21
  • 打赏
  • 举报
回复
下面的帮助写得很清楚
以下示例首先创建一个简单的 DataSet,该数据集包含一个 DataTable,两个列和十个行。通过调用 WriteXml 方法将 DataSet 架构和数据写入磁盘。创建另一个 DataSet,使用 ReadXml 方法为其填充架构和数据。

[Visual Basic, C#] 注意 此示例显示如何使用 ReadXml 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
' Create a DataSet with one table and two columns.
Dim OriginalDataSet As New DataSet("myDataSet")
OriginalDataSet.Namespace = "NetFrameWork"
Dim myTable As New DataTable("myTable")
Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
c1.AutoIncrement = True
Dim c2 As New DataColumn("item")
myTable.Columns.Add(c1)
myTable.Columns.Add(c2)
OriginalDataSet.Tables.Add(myTable)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = myTable.NewRow()
newRow("item") = "item " + i.ToString()
myTable.Rows.Add(newRow)
Next i
OriginalDataSet.AcceptChanges()
' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(OriginalDataSet, "Original DataSet")
' Write the XML schema and data to file with FileStream.
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter _
(fsWriteXml, System.Text.Encoding.Unicode)
' Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter)
' Close the FileStream.
fsWriteXml.Close()

' Dispose of the original DataSet.
OriginalDataSet.Dispose()
' Create a new DataSet.
Dim newDataSet As New DataSet("New DataSet")

' Read the XML document back in.
' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
' Close the XmlTextReader
myXmlReader.Close()

' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(newDataSet, "New DataSet")
End Sub


Private Sub PrintValues(ds As DataSet, label As String)
Console.WriteLine(ControlChars.Cr + label)
Dim t As DataTable
Dim r As DataRow
Dim c As DataColumn
For Each t In ds.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r In t.Rows
For Each c In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
[C#]
private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.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 XML schema and data to file with FileStream.
string xmlFilename = "myXmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml.Close();

// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");

// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close();

// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。

boyyao 2003-07-21
  • 打赏
  • 举报
回复
reammaster(天涯) ( ) 信誉:100
我使用
DataSet.ReadXml(ss);
提示
完全限定后路径太长。请确保路径少于 260 个字符
dreammaster 2003-07-21
  • 打赏
  • 举报
回复
DataSet.ReadXml(string yourstring)
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-21 03:56
社区公告

让您成为最强悍的C#开发者