简体   繁体   中英

How to parse a Generic XML File in C# to create a Table in SQL Server database

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.

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://docs.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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM