Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, a couple comments on your question:</p> <p>Your XML example doesn't really lend itself to easy answering. Let's change it to something more semantics-friendly (is that a word?):</p> <pre><code>&lt;people&gt; &lt;person&gt; &lt;name&gt;John Doe&lt;/name&gt; &lt;age&gt;21&lt;/age&gt; &lt;/person&gt; &lt;person&gt; &lt;name&gt;Jane Smith&lt;/name&gt; &lt;age&gt;24&lt;/age&gt; &lt;/person&gt; &lt;/people&gt; </code></pre> <p>Your question doesn't really mention what <strong>you</strong> tried to solve the problem. As NovaJoe said, coding this would be quite trivial using Linq-to-XML and Linq-to-SQL, so how do <strong>you</strong> prefer interacting with your database? Have you written C# code in the past to connect to a database? Are you familiar with LINQ? (<em>If not, you should learn it!</em>)</p> <p>To convince you how badly you should go learn the above technologies, if you're not already familiar with them, I whipped up a quick example of Linq-to-XML and Linq-to-SQL, using the excellent program <a href="http://www.linqpad.net/" rel="nofollow noreferrer">LINQPad</a>:</p> <p>I had the following database table:</p> <pre><code>CREATE TABLE [dbo].[People] ( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](max) NOT NULL, [Age] [int] NOT NULL, CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED ( [Id] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] </code></pre> <p>And I used LinqPAD to just open up a standard Linq-to-SQL connection for me: <img src="https://i.stack.imgur.com/qD3FK.png" alt="sample linqpad database connection"></p> <p>Finally, the C# code:</p> <pre><code>// Connect to the database using linq-to-sql var db = new TypedDataContext(); // Load the xml file using linq-to-xml var doc = XDocument.Load(@"C:\path\to\file\people.xml"); var people = doc.Descendants("person"); foreach (var person in people) { // Create person object var personObject = new People(); personObject.Name = person.Descendants("name").Single().Value; personObject.Age = Convert.ToInt32(person.Descendants("age").Single().Value); // Queue to add person to database db.People.InsertOnSubmit(personObject); } // Finally, submit any pending changes db.SubmitChanges(); </code></pre> <p>This resulted in the following data going into my database:</p> <p><img src="https://i.stack.imgur.com/dNIEv.png" alt="table with data"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload