Pass XML file to SQL stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yingwen
    New Member
    • Apr 2007
    • 17

    #1

    Pass XML file to SQL stored procedure

    I Wrote a stored procedure in SQL Server:

    CREATE PROCEDURE InsertXML2 @xmlFile varchar(2000) AS
    DECLARE @iTree int
    SELECT *
    FROM OPENXML (@iTree, 'n:Registration/nefaultEnrolmen t',3)
    WITH (
    ....
    )
    EXEC sp_xml_removedo cument @iTree
    GO

    How can I pass the XML file to this stored Procedure in VB.NET?

    Thanks a lot
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Do following :


    [PHP]
    EXEC InsertXML2 'text of XML File here'[/PHP]

    Comment

    • iburyak
      Recognized Expert Top Contributor
      • Nov 2006
      • 1016

      #3
      I don't think your procedure works thou.

      But if yes then, sorry.

      Comment

      • yingwen
        New Member
        • Apr 2007
        • 17

        #4
        It works fine in following code:

        CREATE PROCEDURE InsertXML AS
        DECLARE @iTree int
        DECLARE @xmlFile varchar(2000)
        SET @xmlFile='<?xml version="1.0" ?>
        <Registration

        </Registration>'

        --Create an internal representation of the XML document.
        EXEC sp_xml_prepared ocument @iTree OUTPUT, @xmlFile,
        '<root xmlns:n="NZL:go vt:IRD:KiwiSave r:B2BInterfaces :RegisterMember :v1.0"
        xmlns:xAL="urn: oasis:names:tc: ciq:xal:3"
        xmlns:xNL="urn: oasis:names:tc: ciq:xnl:3"
        xmlns:xPIL="urn :oasis:names:tc :ciq:xpil:3"
        xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocat ion="NZL:govt:I RD:KiwiSaver:B2 BInterfaces:Reg isterMember:v1. 0 ../RegisterMember. xsd"/>'

        -- Execute a SELECT statement that uses the OPENXML rowset provider.
        SELECT *
        FROM OPENXML (@iTree, 'n:Registration/n:DefaultEnrolm ent',3)
        WITH (
        DateSent datetime './@DateSent',
        Number decimal 'n:Scheme/@Number',
        ActiveType char(2) 'n:Scheme/@ActiveType',
        AddressLine1 varchar(30) 'child::n:Membe rAddress/xAL:FreeTextAdd ress/xAL:AddressLine[position() = 1]',
        AddressLine2 varchar(30) 'child::n:Membe rAddress/xAL:FreeTextAdd ress/xAL:AddressLine[position() = 2]',

        BirthDateTime datetime 'xPIL:BirthInfo/@xPIL:BirthDate Time',
        EmployerName varchar(20) 'n:Employers/n:Employer/n:OrganizationN ame/xNL:NameElement/text()'
        )
        EXEC sp_xml_removedo cument @iTree
        GO

        How can I pass the xml file into
        CREATE PROCEDURE InsertXML @fileName varchar(2000) AS

        in VB.NET?


        Thanks in advance

        Comment

        • iburyak
          Recognized Expert Top Contributor
          • Nov 2006
          • 1016

          #5
          What server do you use?

          Comment

          • iburyak
            Recognized Expert Top Contributor
            • Nov 2006
            • 1016

            #6
            It should be something like this:

            1. Create proc which accepts parameter as a varchar:

            [PHP]CREATE PROCEDURE InsertXML
            @xmlFile varchar(8000)
            AS
            DECLARE @iTree int

            ............[/PHP]


            2. Call procedure from vb.net

            [PHP]object.execute "InsertXML '<?xml version="1.0" ?>
            <Registration >

            </Registration>'"[/PHP]


            You can store text of an XML File in a variable first

            [PHP]object.execute "InsertXML '" & XML_Variable_He re & "'" [/PHP]


            Good Luck.

            Comment

            • yingwen
              New Member
              • Apr 2007
              • 17

              #7
              Originally posted by iburyak
              What server do you use?

              I am using SQL Server 2000

              Comment

              • yingwen
                New Member
                • Apr 2007
                • 17

                #8
                Thank you very much for your quick responses.

                I have another problem here. How can I turn a XML file into a xml string.

                I have tried streamreader, xmltextreader, the out put results only give me the text() part of xml file(all xml tags are not included).

                In other words How can I convert xml file into
                A string =
                "<?xml version="1.0"?> <Registration
                xmlns="NZL:govt :IRD:KiwiSaver: B2BInterfaces:R egisterMember:v 1.0"
                xmlns:xAL="urn: oasis:names:tc: ciq:xal:3"
                xmlns:xNL="urn: oasis:names:tc: ciq:xnl:3
                </Registration>"

                Thank you very much.

                Comment

                • iburyak
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1016

                  #9
                  Just open file like any text file, don't worry that it is an XML and get entire text into a variable.

                  If you use Server 2000 you have a danger to overflow maximum size of a character variable which is 8000. I managed to work around this problem but it requires some programming skills on your side.

                  SQL doesn't really care of any name spaces as long as it is well formed document with root node and children nodes.

                  I have to go now. Will talk tomorrow if you need more help.

                  Have a good day.

                  Comment

                  • yingwen
                    New Member
                    • Apr 2007
                    • 17

                    #10
                    Thank you very much for your reply.


                    I have tried streamreader, xmldocument, xmlreader, but they only read the text part of xml.

                    E.g. for a simple xml file:

                    <Root>
                    <book>title1</book>
                    </Root>

                    The output is only "title1", but I want the whole thing.

                    The problem is how can I read (more complicated than above) entire xml file into a string?

                    Thank you.

                    Comment

                    • iburyak
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1016

                      #11
                      This code works fine:

                      [PHP]Dim sr As StreamReader = File.OpenText(" C:\test.xml")
                      Dim str As String = sr.ReadToEnd()[/PHP]

                      Comment

                      Working...