简体   繁体   中英

How to read xml data into a DataSet in c#?

Im new to .net c# programming and i need to read remote xml file into dataset and create crystal report with the dataset.

so far everything works fine except some Unicode characters showing incorrectly in crystal report viewer

so is this the correct way to load xml file which contains unicode?

string reportDataPath = "http://domain/test/data.xml";

DataSet reportData = new DataSet();
try
{
  reportData.ReadXml(reportDataPath);
}
catch
{

}

to set source

report = new SampleReport();
report.SetDataSource(reportData);

in xml file encoding set like

<?xml version="1.0" encoding="UTF-8"?>

edit:-

this is the problem im talking about. this is with sinhala unicode font

text in xml file is showing below

在此处输入图片说明

crystal report viewer shows below text

在此处输入图片说明

Regards

Typically I always try to be explicit with my encoding types so I would do it like this:

System.Data.DataSet reportData = new System.Data.DataSet();
System.Net.WebRequest request= System.Net.WebRequest.Create(reportDataPath);

using (System.Net.WebResponse response =   (System.Net.HttpWebResponse)request.GetResponse()) {
    using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8)) {
        reportData.ReadXml(sr);
    }
}

Just be aware that unicode can be encoded using different formats, UTF-8, UTF-16, etc. Mostly what you'll see is UTF-8. In .NET the encoding type Encoding.Unicode is UTF-16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM