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

用XmlTextWriter写XML的编码问题

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

由于要用C#从Delphi写的Web Service端获取Delphi的TClientDataSet保存的XML,取回来的数据为了方便以及快速查找,当然是放到.Net的DataSet里了,但两个工具使用的XML格式不一样,咋办?转呗,还有撒说的,打开Delphi写的Web Service端获取的XML分析了一下,再打开.Net的DataSet生成的XML发现.Net的DataSet所需的XML格式如果不带schema就超简单,OK,下步开始转了.下面是转换的方法,

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

StringWriter vStringWriter = new StringWriter();
XmlTextWriter vXmlTextWriter = new XmlTextWriter(vStringWriter);
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 = vStringWriter.ToString();
vStringWriter.Close();
return vResult;
}

测试一下.转换成功,打开一看.NND,发现XML头是<?xml version="1.0" encoding="utf-16"?>

编码咋成Utf16了,居然默认是Utf16的.回头检查一下发现StringWriter不让改编码,XmlTextWriter创建的时候也不支持对StringWriter改编码,想了下也对,用StringWriter直接生成字符串了咋转,只能改成用流的方法了

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;
}

改成这样,再测试,再打开,OK,XML文件的头的编码也变成<?xml version="1.0" encoding="utf-8"?>

了,

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