How to check xml file though .net? + How do I do this if I use XML serialization?

I want users to be able to export data as an XML file. Of course, I want them to be able to import the same XML file afterwards, but they could always change it or it could be a different XML file.

So, I want to check the XML file to see if it matches the format I expect. Therefore, I think I will need something like a circuit to verify that this should be through code.

So if I expect

<Root>
 <Something>
    <SomethingElse> </SomethingElse>
 </Something>
</Root>

I don’t want some other format to be in the file, and then the one that I expect.

Also how can I check the fields? For example, I require that there be some text between the tags. If it is empty, the file is invalid.

So how can I do this?

Edit

XML, , , , . , # xml- .

xml- xml, ? , - , , ? ?

+3
5

, :

using (FileStream stream = File.OpenRead(xsdFilepath))
{
    XmlReaderSettings settings = new XmlReaderSettings();

    XmlSchema schema = XmlSchema.Read(stream, OnXsdSyntaxError);
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas.Add(schema);
    settings.ValidationEventHandler += OnXmlSyntaxError;

    using (XmlReader validator = XmlReader.Create(xmlPath, settings))
    {
        // Validate the entire xml file
        while (validator.Read()) ;
    }
}

OnXmlSyntaxError .

+10

XML . XML- DTD, XDR XSD Visual #.NET

. XML XSD #

: XML XSD, #

:

public void ValidateXmlDocument(
    XmlReader documentToValidate, string schemaPath)
{
    XmlSchema schema;
    using (var schemaReader = XmlReader.Create(schemaPath))
    {
        schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
    }

    var schemas = new XmlSchemaSet();
    schemas.Add(schema);

    var settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = schemas;
    settings.ValidationFlags =
        XmlSchemaValidationFlags.ProcessIdentityConstraints |
        XmlSchemaValidationFlags.ReportValidationWarnings;
    settings.ValidationEventHandler += ValidationEventHandler;

    using (var validationReader = XmlReader.Create(documentToValidate, 
           settings))
    {
        while (validationReader.Read()) { }
    }
}

private static void ValidationEventHandler(
    object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Error)
    {
        throw args.Exception;
    }
    Debug.WriteLine(args.Message);
}
+9
0
source

You can use xml serialization in your classes:

[XmlType("Root", Namespace = "http://example.com/Root")]
[XmlRoot(Namespace = "http://example.com/Root.xsd", ElementName = "Root", IsNullable = false)]
public class Root {
  [XmlElement("Something")] 
  public Something Something { get; set; }
}

public class Something {
  [XmlElement("SomethingElse")]
  public SomethingElse SomethingElse { get; set; }
}

public class SomethingElse {
  [XmlText]
  public string Text { get; set; }
}

Serialize it as follows:

var serializer = new XmlSerializer(typeof(Root));
serializer.Serialize(outputStrem, myRoot);

Then you can check before deserializing, for example:

var serializer = new XmlSerializer(typeof(Root));
string xml = @"
  <Root xmlns='http://example.com/Root'>
    <Something>
      <SomethingElse>Yep!</SomethingElse>
    </Something>
  </Root>"; // remember to use the XML namespace!
Debug.Assert(serializer.CanDeserialize(new XmlTextReader(new StringReader(xml))));

And then just deserialize:

Root newRoot = (Root)serializer.Deserialize(inputStream);

Your XSD is implicit. This matches your classes. To use richer XSDs, you can take a chance at Schema Providers .

0
source

Source: https://habr.com/ru/post/1729435/


All Articles