mehrdad
mehrdad

Reputation: 357

read xml in c# and convert to list

i wanna read this xml and convert it to list for inserting into database

please help

<?xml version="1.0" encoding="UTF-8"?>
-<xml>-<records>-<record><database path="J:\EndNote\PA-03\1.enl" name="1.enl">1.enl</database><source-app name="EndNote" version="14.0">EndNote</source-app><rec-number>38</rec-number>-<foreign-keys><key db-id="r50aeetv1taarsewtwsvrr2h2wtzde5z25pp" app="EN">38</key></foreign-keys><ref-type name="Journal Article">17</ref-type>-<contributors>-<authors>-<author><style size="100%" font="default" face="normal">Patrick Carpenter</style></author></authors></contributors>...

my xml schema

EDIT:

its for Exported EndNote

its so messy and it does not do the trick

please check this

full xml link

Upvotes: 0

Views: 1039

Answers (2)

fan711
fan711

Reputation: 716

Create classes for the model of the XML contents such as:

class Records
{
    public List<Record> records { get; set }
}

class Record
{
    public RecordDatabase database { get; set }
    ...
    public int rec_number { get; set }
    ...
}

class RecordDatabase
{
    public string path { get; set }
    public string name { get; set }
}

...

Then you can deserialize your XML File into an object:

Records records = null;
string path = "importfile.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));
StreamReader reader = new StreamReader(path);
reader.ReadToEnd();
records = (records)serializer.Deserialize(reader);
reader.Close();

Upvotes: 1

Giedrius
Giedrius

Reputation: 8550

Quickest way to do this is to use xsd.exe to generate serialization classes :

1) generate schema definition

xsd file.xml [/outputdir:directory]

This step is not needed, if xml supplier has xsd schemas allready (sample xml does not have defined any used xsd schemas)

2) generate classes from xsd schema:

xsd file.xsd {/classes | /dataset} [/element:element]
         [/language:language] [/namespace:namespace]
         [/outputdir:directory] [URI:uri]

3) use xml serializer to deserialize from generated classes:

http://msdn.microsoft.com/en-us/library/dsh84875.aspx

Upvotes: 2

Related Questions