0

I need to save into sql server the data from a xml file with output like: with root casts:

<casts>
   <dirfilms>
      <dirid>AaB</dirid>
      <is>A.Bennett</is>
      <castnote>Actors and notes.</castnote>
      <filmc>
         <m>
            <f>AaB10</f>
            <t>A Private Function</t>
            <a>Michael Palin</a>
            <p>Und</p>
            <r>podiatrist</r>
         </m>
         <m>
            <f>AaB10</f>
            <t>A Private Function</t>
            <a>Maggie Smith</a>
            <p>Und</p>
            <r>ambitious wife</r>
            <awards>
               <award>BFA</award>
            </awards>
         </m>
         <m>
            <f>AaB10</f>
            <t>A Private Function</t>
            <a>Denholm Elliott</a>
            <p>Und</p>
            <r/>
            <awards>
               <award>BFA</award>
            </awards>
         </m>
         <m>
            <f>AaB10</f>
            <t>A Private Function</t>
            <a>Richard Griffiths</a>
            <p>Und</p>
            <r/>
         </m>
         ... (more nodes snipped) ...
      </filmc>
   </dirfilms>
</casts>
2
  • 1
    Do you want to save the whole XML as one piece, or are you intending to "shred" that information into relational tables for storage? Commented Dec 29, 2010 at 16:39
  • Yes Marc I would like to save them into relational tables for storage for the case mentioned above, such us: For each director a header entry is provided with {code, name, note} and for each film we have another table let say tblMovie {filmcode, title, actor, followed by dep. info, role-type, role-description, and optional role-name, role-specs}...something similar... Commented Dec 30, 2010 at 7:35

2 Answers 2

1

My suggestion for SQL Server 2008:

XDocument xmlDoc = XDocument.Load(@"C:\myXmlFile.xml");

using (SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"))
{
    conn.Open();

    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO MyTable (MyXmlColumn) VALUES (@XmlColumnParam);";
        cmd.Parameters.AddWithValue("@XmlColumnParam", xmlDoc.ToString());

        cmd.ExecuteNonQuery();
    }
}

The column MyXmlColumn should be nvarchar(MAX) or better XML to keep sure that data that are inserted as valid XML.

Sign up to request clarification or add additional context in comments.

Comments

1

Use XML as the column type in the database. Make a linq2sql mapping (.dbml file) and use something like:

        XElement xml = XElement.Load(@"c:\myXml.xml");
        using (var context = new MyDataContext(connectionStr))
        {
            var entity = new MyTable{XML = xml};
            context.MyTables.InsertOnSubmit(entity);
            context.SubmitChanges();
        }

1 Comment

Dear Hans, your suggestions are very clever, but I am not able to link 2 sql to different tables, like I had explained to Marc above, "I would like to save them into relational tables for storage for the case mentioned above, such us: For each director a header entry is provided with {code, name, note} and for each film we have another table let say tblMovie {filmcode, title, actor, followed by dep. info, role-type, role-description, and optional role-name, role-specs}...something similar" ... your answer is appreciated...thank you once again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.