0

I have a TCP network protocoll where the informations/data are sent as xml documents. Now I would like to match these informations against multiple xsd files. The data is beeing received randomly, so I would like to know which information is coming by validating them against my xsd files.

I have already created the xsd files (~25) which should match

In the second step, I would like to create an instance of a customClass based on the matched xsd file.

The first part of my question has already been answered on SO. For my second question, I need some help.

1 How to validate a xml against multiple xsd

private void _LoadSchemaSet()
{
    _Logger.Debug("Loading schemata..");
    var assembly = Assembly.GetExecutingAssembly();
    var specs = assembly.GetManifestResourceNames().Where(r => r.ToLower().StartsWith("myCompany.myCustomer.specs") && r.EndsWith(".xsd"));
    foreach (string spec in specs)
    {
        _Logger.Trace(spec);
        _SchemaSet.Add(XmlSchema.Read(assembly.GetManifestResourceStream(spec), _Schema_ValidationEventHandler));
    }
}


public bool ValidateXDocument(XDocument document)
{
    bool result = true;
    document.Validate(_SchemaSet, (sender, args) => { result = false; });

    // at this point I would like to get the xsd file which matches

    return result;
}

2 How to get the xsd file which matches the xml file

Community
  • 1
  • 1
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • Either the XML file has a schema specified in the file or you have to manually build he schema from scratch. – jdweng Apr 08 '19 at 09:22
  • I have updated my question, with some more infos. The XML does not have any schema informations. I have already build my ~25 schemata and would like to match the unknown xml against them. In second step I parse it into a `customClass` based on the xsd file – Dominic Jonas Apr 08 '19 at 09:30
  • What I usually suggest is loading the c# classes with test data and then serialize to produce an xml file. Then compare the unknown xml against the serialized xml. – jdweng Apr 08 '19 at 09:35

0 Answers0