I have an XML file with SHIFT-JIS encoding as below:
<?xml version="1.0" encoding="SHIFT-JIS" standalone="yes"?>
<海外管理ファイル><PO番号>GV05097</PO番号><データベース><PO><Tbl_PO_H PO番号="GV05097"><DATA><PO番号 TYPE="200" LENGTH="13">GV05097</PO番号></DATA></Tbl_PO_H></PO></データベース></海外管理ファイル>
And I use SQL store procedure to insert it into SQL table:
alter PROCEDURE [dbo].[proc_TBL_PO_H_LoadXMLPO]
@xml XML
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [ENVIETNAMPO].[dbo].[TBL_PO_H]
SELECT
TBL_PO_H.value('(PO番号/text())[1]','varchar(13)') AS PO番号, --TAG
TBL_PO_H.value('(PO発行日/text())[1]','varchar(10)') AS PO発行日
FROM
@xml.nodes('/海外管理ファイル/データベース/PO/Tbl_PO_H/DATA')AS TEMPTABLE(TBL_PO_H)
END
C# code for Load XML button:
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDesktop = Path.Combine(pathUser, "Desktop");
var xmlfilename = string.Empty;
var xmlfilePath = string.Empty;
//var dt = new DataTable();
var sqlConn = new SqlConnection(strConStr);
try
{
openFileDialog1.InitialDirectory = @pathDesktop;
openFileDialog1.Title = "Browse XML PO File";
openFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.ShowHelp = true;
openFileDialog1.FileName = "*.xml";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
xmlfilename =openFileDialog1.SafeFileName;
xmlfilePath = pathDesktop +"\\"+ xmlfilename;
string xml = File.ReadAllText(xmlfilePath, Encoding.GetEncoding("SHIFT-JIS"));
sqlConn.Open();
var cmd = new SqlCommand("proc_TBL_PO_H_LoadXMLPO", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@xml", xml);
SqlCommand arithabortCommand = new SqlCommand("SET ARITHABORT ON", sqlConn);
arithabortCommand.ExecuteNonQuery();
cmd.ExecuteNonQuery();
sqlConn.Close();
}
MessageBox.Show("PO XML File has been imported successfully.", "Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
But when loading, an error occur as the following, I tried to read XML file with correct encoding as the XML file. Please help me. Thank you!
System.Data.SqlClient.SqlException 0x80131904: parsing XML Line 1, character 59. Cannot swicth I- code ....