S
S
Samir Kurbanov2020-10-12 22:54:44
C++ / C#
Samir Kurbanov, 2020-10-12 22:54:44

How to work with XML file in C# application?

Good evening. I have a folder of XML files with many elements, nestings, and complex structure that need to be written into several related tables. But before recording, it is necessary to check the XML for the correctness of the structure, the conformity of the data types, the contained elements, the presence of the obligatory. elements, etc.
This task is solved by checking xml files according to the xsd schema.
But I heard there is another way to implement such a check by writing C # classes with variables corresponding to xml elements, something like:

<xml>
    <name>Samir</name>
    <dr>1996-09-16</dr>
    <okato>82000</okato>
</xml>

C# class of this xml:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
  [XmlRoot(ElementName="xml")]
  public class Xml {
    [XmlElement(ElementName="name")]
    public string Name { get; set; }
    [XmlElement(ElementName="dr")]
    public DateTime Dr { get; set; }
    [XmlElement(ElementName="okato")]
    public string Okato { get; set; }
  }
}

The question is, is it possible to check the presence of mandatory / extra elements in the checked xml file in the second way?
How it is possible to check xml-element with above-described class?
Is validating against an XSD schema a more performant or convenient option?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-10-12
@zagayevskiy

Let's say you validate xml against a schema.. and then what? To decompose it into a database, you still need to parse it.

S
Samir Kurbanov, 2020-10-12
@kurbanov_samir

@agayevskiy, No, further xml-ku is waiting for even more in-depth checks for compliance with NSI (directories), checking for the existence of certain elements in the database, etc. And this check will be carried out at the level of stored procedures (T-SQL), to which the file will be transferred as an HP parameter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question