c#中DataTable 如何转换为xml信息呢?


摘要:
下文讲述将DataTable转换为xml格式的字符串的方法分享,如下所示;

实现思路:
    使用XmlSerializer即可将DataTable转换为xml

例:

 
public string dataTableToXml(DataTable tb)
{
 StringBuilder str = new StringBuilder();
    XmlWriter xmlInfo = XmlWriter.Create(str);
    XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
    serializer.Serialize(xmlInfo, tb);
    xmlInfo.Close();
    return str.ToString();   
}