返回首页
当前位置: 主页 > 编程语言 > XML教程 >

将Delphi的TClientDataSet生成的XML文件转成.Net的DataSet可以识

时间:2015-11-21 22:58来源:电脑教程学习网 www.etwiki.cn 编辑:admin

费话就不说了,转换方法如下,注意这个方法转换的时候没有生成schema,如果要生成schema稍有点麻烦呢

private string ReadDelphiDataSetToNetDataSet(string vDelphiStr, string vTableName)
{
string vResult = vDelphiStr;
XmlTextReader reader = new XmlTextReader(vDelphiStr, XmlNodeType.Document, null);
reader.Read();
MemoryStream stream = new MemoryStream();

XmlTextWriter vXmlTextWriter = new XmlTextWriter(stream, new UTF8Encoding());
vXmlTextWriter.Formatting = Formatting.Indented;
vXmlTextWriter.WriteStartDocument();
vXmlTextWriter.WriteStartElement("NewDataSet");

while (!reader.EOF)
{
if ((reader.NodeType == XmlNodeType.Element && reader.Name.ToUpper() == "ROW"))
{
vXmlTextWriter.WriteStartElement(vTableName);
if (reader.HasAttributes)
{
if (reader.MoveToFirstAttribute())
{
vXmlTextWriter.WriteElementString(reader.Name, reader.Value);
while (reader.MoveToNextAttribute())
{
vXmlTextWriter.WriteElementString(reader.Name, reader.Value);
}
}
//reader.ReadEndElement();
}
vXmlTextWriter.WriteEndElement();
}
reader.Read();
}

vXmlTextWriter.WriteEndElement();
vXmlTextWriter.WriteEndDocument();
vXmlTextWriter.Flush();
vXmlTextWriter.Close();
reader.Close();
vResult = Encoding.UTF8.GetString(stream.ToArray());
stream.Close();
return vResult;
}

------分隔线----------------------------
标签(Tag):xml
------分隔线----------------------------
推荐内容
猜你感兴趣