Search code examples
c#sql-serverxmlvisual-studioentity-framework

Convert file content from XML file to UTF-16


In a C# console app I'm reading XML files which are in a specific folder.

I'm getting the content from each file as a string and want to write it to a SQL Server table using Entity Framework.

The table has three columns: the first is an automatically incremented ID, the second a GUID and the third the contents of the XML file.

The column for the content of the XML file is of type XML.

When I try to write a row into that table with the content of a XML file, I'm getting the following exception:

XML parsing: line 1, character 38, unable to switch the encoding

The XML file has encoding UTF-8, but I think I need UTF-16.

How can I encode the file content in the right way?

Any help is welcome.

This is a typical header of the XML files:

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

The table was created in the following way:

USE [myDatabase]
GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [stor].[myTable](
    [myTableId] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [FileGuid] [varchar](36) NOT NULL,
    [XmlDocument] [xml] NOT NULL,
 CONSTRAINT [PK_myTable] PRIMARY KEY CLUSTERED 
(
    [myTableId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

And this is my code for writing the file content to SQL Server:

public void WriteXmlTestFileToDatabase(string xmlFileContent)
{
    using (SqlServerDatabaseEntities db = new SqlServerDatabaseEntities())
    {
        var newEntry = new myTable();
        newEntry.FileGuid = Guid.NewGuid().ToString();
        newEntry.XmlDocument = xmlFileContent;

        db.myTable.Add(newEntry);
        db.Entry(newEntry).State = EntityState.Added;

        db.SaveChanges();   // Here I'm getting the exception
    }
}

CONCLUSION

I have removed the first node with the encoding and used SqlCommand for writing to the database.

So it's finally working.

Thank you all for your assistance and the input.

That's why i like this forum.

Thank you very much! :)


Solution

  • After testing, I confirmed that this issue does occur, but it can be resolved by removing the XML declaration. Removing the declaration does not affect the validity of the XML file, and the document can still be correctly parsed and processed by the XML parser.

    var filecontext=File.ReadAllText("C:\\Users\\TestUser\\Desktop\\1.xml");
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(filecontext);
    if (xmldoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
    {
        xmldoc.RemoveChild(xmldoc.FirstChild);
    }
    

    This code reads an XML file, parses its content, checks if an XML declaration node (e.g., ) exists, and removes it if present.

    The FirstChild property is used to access the first node of the XML document. If this node is an XML declaration, its NodeType will be XmlNodeType.XmlDeclaration.