CSharp · Console
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Reflection;
using System.Xml.Schema;
using NUnit.Framework; //NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/63329041/how-to-parse-xml-file-in-c-sharp-to-create-a-table-in-sql-server-database
class TestClass
{
public static void Test()
{
var xsdString = GetXsd();
// 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);
}
}
}
}
}
static string GetXsd()
{
return @"<?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>";
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
public static string GetNetCoreVersion()
{
//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
//
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}
}