4 Replies - 1934 Views - Last Post: 14 May 2014 - 03:38 AM

#1 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 17-October 11

Importing xml files, storing in db and grabbing from db

Posted 22 April 2014 - 09:21 AM

Hi, I'm new to C#, WPF and XML importation and databases. So there might be a few things I don't know anything about, so I thought to ask to clear up any misconceptions. I will tell you what I want to do and what I have done so far. I just want this to be a conceptual discussion although some code here and there is fine too for defining concepts. Not looking for a hard set in stone answer. Just looking for advice on what to read or how to conceptually think about how the data is transferred from importation to database and back on the view.

What I want to do:
-Have the user browse for a xml file, select it and have it save into the database.
-Have the xml file that was browsed and selected display all it's data on a DataGrid after
browsing for the file.
-Have another datagrid in the page that contains all previously uploaded xml files.
-In the imported xml files, I want to validate that the imported does not
already exist.

What I have done:
-Used the OpenFileDialog to have the user browse for an xml file, select it and display the path in a textfield.
-Created classes based on the XML file
-Attached (call) a deserialization method to browse file path
-Set up tables in my DbContext using DbSet.
-Started writing a method that contains a select query to grab data from database to display on datagrid.

So far, when I used a select query to see if there's any data stored in the database after having a user select an xml file.... there's no data stored. I'm not sure why? What am I missing? How do I grab the data from the xml file and store it in the database? I thought deserializing it would work. Perhaps, I'm missing a logical step here. What, in words, is typically done to import xml files, store the data in a database and grab the data onto the view? Conceptually, what are the steps involved in something like this? This will allow me to cross-check myself.

This post has been edited by Eowyn27: 22 April 2014 - 09:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Importing xml files, storing in db and grabbing from db

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,673
  • Joined: 02-June 10

Re: Importing xml files, storing in db and grabbing from db

Posted 22 April 2014 - 04:30 PM

See FAQ # 32 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 17-October 11

Re: Importing xml files, storing in db and grabbing from db

Posted 23 April 2014 - 05:22 AM

Here is what I have tried so far:

lotinformation.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<lot_information lot_exp_date="XXXX-XX-XX" lot_number="123456">
<components> 
<component control="A" ref="A" family="XXXXX" component="12" id="1A1">
<target>42</target> 
<min>12</min >
<max>90</max>
<number>10</number> 
</component>
<component control="A" ref="A" family="XXXXX" component="13" id="1A2B3C">
<target>42</target>
<min>12</min>
<max>90</max>
<number>10</number>
</component>
</components> 
<families>
<control family="XXXXX" ctrl="ctrlA"/> 
<control family="XXXXX" ctrl="ctrlB"/>
<control family="XXXXX" ctrl="ctrlC"/> 
<control family="XXXXX" ctrl="ctrlD"/> 
</families> </lot_information>



lotinformation.cs
[Serializable]
    public class LotInformation
    {   
        
        [Key]
        public int Id { get; set; }

        [XmlElement]
       public virtual ICollection<Components> components { get; set; }
        //public Components[] components { get; set; }

        [XmlElement]
        public virtual ICollection<Families> families { get; set; }
       // public Families[] families { get; set; }

        [XmlAttribute("exp_date")]
        public DateTime exp_date { get; set; }

       [XmlAttribute("lot_number")]
        public int lot_number {get; set;} 

    }
    [Serializable]
    public class Components {

        [Key]
         public int Id {get; set;}

        [XmlAttribute("control")]
        public Array[] control { get; set; }

        [XmlAttribute("ref")]
        public string ref { get; set; }

        [XmlAttribute("family")]
        public string family { get; set; }

        [XmlAttribute("component")]
        public int component { get; set; }

        [XmlAttribute("id")]
        public string componentId { get; set; }
        
        [XmlElement]
       public virtual ICollection<ComponentControl> ComponentControl { get; set; }
        //public ComponentControl[] ComponentControl { get; set; }
    
    }
    [Serializable]
    public class Family
    {
           [Key]
        public int Id { get; set; }

         [XmlAttribute("family")]
        private string controlFamily { get; set; }

         [XmlAttribute("ctrl")]
        private string ctrl { get; set; }

    } 

    [Serializable]
    public class ComponentControl
    {

        [Key]
        public int Id { get; set; }

        [XmlElement("target")]
        public int target { get; set; }

        [XmlElement("=min")]
        public int min { get; set; }

        [XmlElement("max")]
        public int max{ get; set; }

        [XmlElement("number")]
        public int number { get; set; }

    }
}


lotviewmodel.cs

public static void DeSerializationXML(string filePath)
        {
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "lot_information";
            xRoot.IsNullable = false;
            // Create an instance of class.
            LotInformation[] lot;
            // Create an instance of stream writer.
            TextReader txtReader = new StreamReader(filePath);
            // Create and instance of XmlSerializer class.
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation[]), xRoot);
            // DeSerialize from the StreamReader
            lot = (LotInformation[])xmlSerializer.Deserialize(txtReader);
            // Close the stream reader
            txtReader.Close();
            Console.ReadLine();

        }

     private string lotFileCreationDirectory;

     private const string LOT_FILE_CREATION_DIRECTORY = "LotFileCreationDirectory";
        public string LotFileCreationDirectory
        {
            get
            {
                return lotFileCreationDirectory;
            }
            set
            {
                lotFileCreationDirectory = value;
                RaisePropertyChanged(LOT_FILE_CREATION_DIRECTORY);
            }
        }

        private void ChangeLotFilePath()
        {
            OpenFileDialog Dialog = new OpenFileDialog();
            Dialog.Filter = "XML files (*.xml)|*.xml";
            Dialog.ShowDialog();

            if (!String.IsNullOrEmpty(Dialog.FileName))
            {
                LotFileCreationDirectory = Dialog.FileName;
                DeSerializationXML(Dialog.FileName);
            }
 
        }

        public ICommand ChangeLotFilePathCommand
        {
            get
            {
                return new RelayCommand(ChangeLotFilePath, null);
            }
        }



datacontext.cs

class DDataContext : DbContext
    {
        public DbSet<Components> Components{ get; set; }
        public DbSet<ComponentControl> ComponentControl { get; set; }
        public DbSet<Families> Families { get; set; }
        public DbSet<LotInformation> LotInformation {get;set;}
        
        public DDataContext() : base("DConnectionString")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }



Here's the most important code I think. Just wondering what's wrong with my deserialization method because I think that
is where the problem is. Also, I think I was running into problems using virtual tables before but I think it has since been fixed (since I see no more errors in VS).

The problem I'm having right now is that the data from the xml is not going into the database. What step in the process am I missing? I think I'm just missing something conceptually along the way.

This post has been edited by Eowyn27: 23 April 2014 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Importing xml files, storing in db and grabbing from db

Posted 13 May 2014 - 07:55 PM

Where is the code that tries to put stuff into the database?
Was This Post Helpful? 0
  • +
  • -

#5 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 17-October 11

Re: Importing xml files, storing in db and grabbing from db

Posted 14 May 2014 - 03:38 AM

View PostSkydiver, on 13 May 2014 - 09:55 PM, said:

Where is the code that tries to put stuff into the database?


So, I figured out how to put things in the database and retrieve items from db. Also, I read your post from the other thread. Sorry for the confusion. I will try and keep only one thread running next time, just though they were slightly different issues. Thank you so much for your help.

EDIT: Figured it out again. Apparently, it was because my method was static... Thanks :D

This post has been edited by Eowyn27: 14 May 2014 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1