2

I have this Generic XML file, I want to parse it so I can create a SQL server table according to the XML data :

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Student">
  <xs:complexType>
    <xs:sequence>
    <xs:element name="Id" type="xs:integer"/>
    <xs:element name="Name" type="xs:string"/>
    <xs:element name="City" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

I want to recover the following data:

Student, 

Id / integer

Name / String

City / String

Student will be the name of the table, and Id, Name and City are the columns. All I need is how to parse the XML file and recover the data.

KADEM Mohammed
  • 1,650
  • 2
  • 23
  • 45
  • 2
    Does this answer your question? [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c). If you are having a specific problem, edit your question to focus on that. Otherwise it looks like you are just asking for someone to write your code for you. – Mitch Aug 09 '20 at 17:01
  • See also [How to parse XSD file using c#](https://stackoverflow.com/questions/34855024/how-to-parse-xsd-file-using-c-sharp) – Mitch Aug 09 '20 at 17:03
  • Does [How to parse an XSD to get the information from elements using C#?](https://stackoverflow.com/q/11569264/3744182) sufficiently answer your question? – dbc Aug 09 '20 at 17:21
  • 1
    I don't agree this is a proper duplicate of [How do I read and parse an XML file in C#?](https://stackoverflow.com/q/642293/3744182). That question addresses ways to parse any XML file, but OP's file is specifically an XSD file (in XML format). As such there are higher-level tools available to deserialize and interpret such a file. [How to parse XSD file using c#](https://stackoverflow.com/q/34855024/3744182) would seem to be a duplicate but unfortunately it's only answer is a link-only answer referring to MSDN documentation for, once again, generic XML parsing. – dbc Aug 10 '20 at 16:18

1 Answers1

1

Your XML file is an XML Schema Definition (XSD) file that formally describe[s] the elements in an Extensible Markup Language (XML) document. You can load such a file into an XmlSchemaSet schema collection, compile it, and then interrogate the schemas thereby defined to get your main element name and its properties.

Let's say that you have a string xsdString that contains the XSD shown in your question. Then you can load it, compile it, and print out the complex elements and their properties as follows:

// Load the xsdString into an XmlSchemaSet.
// If loading from a file use a StreamReader rather than a StringReader
XmlSchemaSet schemaSet = new XmlSchemaSet();
using (var reader = new StringReader(xsdString))
{
    schemaSet.Add(XmlSchema.Read(reader, null));
}

// Compile the schema
schemaSet.Compile();

// Iterate over the schemas
// Code adapted from https://learn.microsoft.com/en-us/dotnet/standard/data/xml/traversing-xml-schemas
foreach (XmlSchema schema in schemaSet.Schemas())
{
    // Iterate over the complex types in the schema
    foreach (XmlSchemaElement element in schema.Elements.Values)
    {
        var complexType = element.ElementSchemaType as XmlSchemaComplexType;
        if (complexType == null)
            continue;

        Console.WriteLine("Complex element: {0}", element.Name);

        // If the complex type has any attributes, get an enumerator
        // and write each attribute name to the console.
        if (complexType.AttributeUses.Count > 0)
        {
            var enumerator = complexType.AttributeUses.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var attribute = (XmlSchemaAttribute)enumerator.Value;
                var name = attribute.Name;
                var type = attribute.AttributeSchemaType.TypeCode;

                Console.WriteLine("   Attribute {0}: {1}", name, type);
            }
        }

        // Get the sequence particle of the complex type.
        var sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
        if (sequence != null)
        {
            // Iterate over each XmlSchemaElement in the Items collection.
            foreach (XmlSchemaElement childElement in sequence.Items)
            {
                var name = childElement.Name;
                var type = childElement.ElementSchemaType.TypeCode;

                Console.WriteLine("   Element {0}: {1}", name, type);
            }
        }
    }
}

Which outputs

Complex element: Student
   Element Id: Integer
   Element Name: String
   Element City: String

Note that the XSD type xs:integer corresponds to an unbounded integer, not necessarily one that will fit in an Int32 or Int64.

References:

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340