Avatar of yolish
yolish
 asked on

Loading XML Document

I am trying to load an xml document that has the following format

<System>
  <sysinfo>
    <account>1</account>
  </sysinfo>
  <order>
    <mark>12</mark>
  </order>
  <detail>
     <marking>12-2</marking>
  </detail>
  <order>
    <mark>13</mark>
  </order>
  <detail>
     <marking>13-2</marking>
  </detail>
</system>

I am trying to load this into SQL Server. I have a SPROC that can handle the loading, my problem is getting the XML document into the SQL Server SPROC.

I have done a lot of research and have read use a DataSet, etc... but need some help on the VB.net part of things... Any and all help would be appreciated.

Thanks,
-t
Visual Basic.NET

Avatar of undefined
Last Comment
yolish

8/22/2022 - Mon
LordWabbit

here you go, will need to change the insert to cater for your specific table etc.  also might want to put the code in a loop if you are inserting multiple files

        Dim myReader As New System.IO.StreamReader("C:\eatme\xml.doc")
        Dim strXml As String = myReader.ReadToEnd()
        myReader.Close();
        Dim myConnection As New SqlConnection(strConnectionString)
        myConnection.Open()
        Dim myCommand As New SqlCommand("Insert INTO XmlTable (xmlColumnName) Values (@xmlData)", myConnection)
        myCommand.Parameters.AddWithValue("@xmlData", strXml)
        myCommand.ExecuteNonQuery()
        myCommand.Dispose()
        myConnection.Close()
        myConnection.Dispose()

please not that your xml example is not well formed and sql will throw an exception (closing tag should be </System> xml is case sensitive)  also might be a good idea to create an xsd and assign it to the xml column which will check if the right elements are present in the xml when you insert it into the xml column
yolish

ASKER
Wabbit... Looks good, still have a few questions

1) What is the parameter @xmlData?
2) Lets say I want to enter information into an Orders table that has both the Account (sysinfo) & Mark(order)?

thanks
ASKER CERTIFIED SOLUTION
LordWabbit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
yolish

ASKER
LordWabbit-

Good answer... had to make 1 small change
add
myCommand.CommandType = CommandType.StoredProcedure
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy