S
S
Senia_sweety2021-04-30 21:13:01
SQL
Senia_sweety, 2021-04-30 21:13:01

Converting xml to sql (code first or directly via SQL)?

In general, good day to all, dear Sharpists.
The problem is, there is an XML file from cbr.ru. I would like to transfer the information from the file to SQl Server, so that later I can show the results and (or) the history of changes through MVC. Options that led me nowhere:
1) the OPENROWSET function in SQL, it creates a hyperlink for xml and, accordingly, simply shows the code of the XML file
2) Crutch Code FIrst

using (AppDbContext context = new AppDbContext())
            {

                while (reader.Read())
                {
                    Test test = new Test();
                    reader.ReadStartElement();
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Valute"))
                    {
                        if (reader.HasAttributes)
                        {
                            test.ValuteID = reader.GetAttribute("ID");
                        }
                    }
                    reader.ReadToDescendant("NumCode");
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "NumCode"))
                    {
                        test.NumCode = Convert.ToInt32(reader.ReadInnerXml());
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "CharCode"))
                    {
                        test.CharCode = Convert.ToString(reader.ReadInnerXml());
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Nominal"))
                    {
                        test.Nominal = Convert.ToInt32(reader.ReadInnerXml());
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Name"))
                    {
                        test.Name = Convert.ToString(reader.ReadInnerXml());
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Value"))
                    {
                        test.Value = Convert.ToDecimal(reader.ReadInnerXml());
                    }
                    listTest.Tests.Add(test);
                }
              context.ListTests.Add(listTest);
              await context.SaveChangesAsync();
           }

The problem here is that unnecessary objects are created that do not contain the necessary information, simply dummies.
Classes for transferring data to the database:
public class Test
    {
        public int Id { get; set; }
        public string ValuteID { get; set; }
        public int NumCode { get; set; }
        public string CharCode { get; set; }
        public int Nominal { get; set; }
        public string Name { get; set; }
        public decimal Value { get; set; }
        public ListTest ListTest { get; set; }
    }
    public class ListTest
    {
        public int Id { get; set; }
        
        public List<Test> Tests { get; set; } = new List<Test>();
        
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2021-05-07
@firedragon

Direct xml loading
https://www.mssqltips.com/sqlservertip/5707/simple...
Code execution

using(var context = new SampleContext())
{
    var commandText = "INSERT Categories (CategoryName) VALUES (@CategoryName)";
    var name = new SqlParameter("@CategoryName", "Test");
    context.Database.ExecuteSqlCommand(commandText, name);
}

Deserialization, without unnecessary movements
https://docs.microsoft.com/ru-ru/dotnet/api/system...
In general, there are a lot of options.
public class SqlItem
        {
            public SqlItem() { }

            public SqlItem(XmlItem item)
            {
                Name = item.Name;
            }
            public string Name { get; set; }
        }

            using var ctx = new ApplicationDbContext(new Microsoft.EntityFrameworkCore.DbContextOptions<ApplicationDbContext>());
            var serializer =new XmlSerializer(typeof(XmlItem[]));
            var filename = "test.xml";
            var fs = new FileStream(filename, FileMode.OpenOrCreate);
            var reader = new StreamReader(fs);
            var xmls = (XmlItem[])serializer.Deserialize(reader);
            foreach (var item in xmls)
                ctx.Items.Add(new SqlItem(item));
            ctx.SaveChanges();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question