private string GetDepartmentInfoXML(string DepartmentId)
{
Department d = CommonSrv.LoadObjectById(typeof(Department), DepartmentId) as Department;
XmlNode node = null;
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateNode(XmlNodeType.Element, "Department", "");
xmlDoc.AppendChild(rootNode);
node = xmlDoc.CreateNode(XmlNodeType.Element, "Succeed", "");
node.InnerText = "1";
rootNode.AppendChild(node);
node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", "");
node.InnerText = d.Name;
rootNode.AppendChild(node);
node = xmlDoc.CreateNode(XmlNodeType.Element, "Remark", "");
node.InnerText = d.Remark;
rootNode.AppendChild(node);
return xmlDoc.OuterXml;
}
通过读取数据库中的数据生成XML文件,没有中文时运行正确,但是当数据d.Name或d.Remark中有中文时就会出错,请问如何解决?是不是由于编码的问题呀,动态创建XML如何设置编码?
------解决方案--------------------------------------------------------
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes");
doc.AppendChild(dec);
------解决方案--------------------------------------------------------
doc.CreateXmlDeclaration("1.0", "gb2312", "yes");
------解决方案--------------------------------------------------------
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
可能就是编码的问题