Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / SQL
Reference

Inserting XML Schema in to SQL Server Db.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
31 May 2016CPOL 13.7K   6   3
Problem and Solution

Introduction

I am writing this just for a reference of an issue which i encountered, while inserting an nlog configuration in to SQL server DB.

Background

I was trying to insert an XML schema (NLog Configuration) in to a database field and I got an exception saying "XML parsing: line 1, character 290, undeclared prefix".

I googled a little bit and found the solution for that 

XML Schema

Below is the schema which i wanted to insert in to the DB.

C++
<target  xsi:type="File" name="TestXmlConfigTarget" fileName="C:\temp\logs\log.csv" archiveFileName="C:\temp\logs\test_log.csv.log" archiveEvery="Month" keepFileOpen="false" encoding="UTF-8" maxArchiveFiles="30" archiveNumbering="Rolling" archiveAboveSize="1000000" concurrentWrites="true">
   <layout xsi:type="CsvLayout">
      <column name="DateStamp" layout="${date:format=MM-\dd-\yyyy HH\:mm\:ss K}" />
   </layout>
</target>

Problem SQL Script. 

DECLARE @txml xml= N'<target  xsi:type="File" name="TestXmlConfigTarget" fileName="C:\temp\logs\log.csv" archiveFileName="C:\temp\logs\test_log.csv.log" archiveEvery="Month" keepFileOpen="false" encoding="UTF-8" maxArchiveFiles="30" archiveNumbering="Rolling" archiveAboveSize="1000000" concurrentWrites="true">
      <layout xsi:type="CsvLayout">
        <column name="DateStamp" layout="${date:format=MM-\dd-\yyyy HH\:mm\:ss K}" />       
      </layout>
    </target>'

SELECT @TXML 

While trying to execute i got an exception like below.

Msg 9459, Level 16, State 1, Line 1
XML parsing: line 1, character 290, undeclared prefix.

Analyzed it a bit and I realized that the "xsi:" is the trouble maker.

Solution

To Solve this issue , i added a xmlns name space  in to the schema root.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  in to the schema  and now the SQL looks like 

DECLARE @txml xml= N'<target  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="File" name="TestXmlConfigTarget" fileName="C:\temp\logs\log.csv" archiveFileName="C:\temp\logs\test_log.csv.log" archiveEvery="Month" keepFileOpen="false" encoding="UTF-8" maxArchiveFiles="30" archiveNumbering="Rolling" archiveAboveSize="1000000" concurrentWrites="true">
      <layout xsi:type="CsvLayout">
        <column name="DateStamp" layout="${date:format=MM-\dd-\yyyy HH\:mm\:ss K}" />       
      </layout>
    </target>'

SELECT @TXML 

And this solved my problem..

After fixing the schema issue ,i could able to insert the variable in to appropriate Db field with out any issue ..

Hope this may help some body ..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Currently working as Application Development Consultant in USA
I love writing what i know about and learning from the mistakes which i make...

Comments and Discussions

 
SuggestionPlease update your title Pin
mldisibio1-Jun-16 9:26
mldisibio1-Jun-16 9:26 
GeneralRe: Please update your title Pin
KK Kod1-Jun-16 10:12
KK Kod1-Jun-16 10:12 
GeneralThat sounds familiar Pin
PIEBALDconsult31-May-16 20:02
mvePIEBALDconsult31-May-16 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.