请教:如何将XML字符串直接读入dataset,而不从文件读取

feic 2006-11-04 12:26:41
请教:如何将XML字符串直接读入dataset,而不从文件读取
...全文
266 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
周公 2006-11-06
  • 打赏
  • 举报
回复
xmlString是一段xml格式的字符串。
周公 2006-11-06
  • 打赏
  • 举报
回复
通过ds.Tables[i]来访问。
feic 2006-11-04
  • 打赏
  • 举报
回复
但是昨天我用这个方法的时候dataset里有两个表,却只能将一个表的XML读入dataset

代码如下
//这里是将数据写入字符串
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
this.formDesignControl1.SchemaDataSet.WriteXml(xw, XmlWriteMode.DiffGram);
this.xmlControl1.XML = sb.ToString();

//下面是读取字符串
DataSet ds = new DataSet();
TextReader stringReader = new StringReader(this.xmlControl1.XML);
ds.ReadXml(stringReader);
kjun1983 2006-11-04
  • 打赏
  • 举报
回复
xmlString 是路径吗?
周公 2006-11-04
  • 打赏
  • 举报
回复
/// <summary>
/// 把Xml字符窜转换成DataSet
/// </summary>
/// <param name="xmlString"></param>
/// <returns></returns>
public static DataSet XmlStringToDataSet(string xmlString)
{
DataSet st = new DataSet();
//string dataSetString = "";
using(System.IO.StringReader sr =new StringReader(xmlString))
{
st.ReadXml(sr);
}
return st;
}

这个是我们现在用的,代码少,容易懂。
feic 2006-11-04
  • 打赏
  • 举报
回复
请问这是什么?跟我的问题有什么关系吗?
pfworld 2006-11-04
  • 打赏
  • 举报
回复
using System;
using System.Xml;

namespace prjCx
{
/// <summary>
/// Config 的摘要说明。
/// </summary>
public class Config
{
private String msFileName = null;

public String ConfigFile
{
get
{
return this.msFileName;
}
set
{
if (System.IO.File.Exists(value.Trim()))
{
this.msFileName = value.Trim();
}
}
}

public Config()
{
this.msFileName = String.Empty;
}

public Config(String ConfigFile)
{
this.ConfigFile = ConfigFile.Trim();
}

public bool ReadConfig(String ContentName, out String ContentValue)
{
bool bFlag = false;

ContentValue = String.Empty;

if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}

try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
ContentValue = xmlNode.InnerText;

bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}

return bFlag;
}

/// <summary>
/// 读XML文件
/// </summary>
/// <param name="ContentName">节点名</param>
/// <param name="PropertyName">属性名</param>
/// <param name="PropertyValue">属性值(Ref)</param>
/// <returns></returns>
public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
{
bool bFlag = false;

PropertyValue = String.Empty;

if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}

try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName);

XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);

XmlAttributeCollection xmlAttr = xmlNode.Attributes;

for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
PropertyValue = xmlAttr.Item(i).Value;
bFlag = true;
break;
}
}
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}

return bFlag;
}

public bool WriteConfig(String ContentName, String ContentValue)
{
bool bFlag = false;

if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}

try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
xmlNode.InnerText = ContentValue;

xmlDoc.Save(this.msFileName);

bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}

return bFlag;
}


/// <summary>
/// 写XML文件
/// </summary>
/// <param name="ContentName">节点名</param>
/// <param name="PropertyName">属性名</param>
/// <param name="PropertyValue">属性值</param>
/// <returns></returns>
public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
{
bool bFlag = false;

if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}

try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName);

XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);

XmlAttributeCollection xmlAttr = xmlNode.Attributes;

for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
xmlAttr.Item(i).Value = PropertyValue;
bFlag = true;
break;
}
}

xmlDoc.Save(this.msFileName);

bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}

return bFlag;
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧