如何在 C# 中解析通用 XML 文件以在 SQL 服务器数据库中创建 Table
How to parse a Generic XML File in C# to create a Table in SQL Server database
我有这个通用 XML 文件,我想解析它以便根据 XML 数据创建一个 SQL 服务器 table :
<?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>
我想恢复以下数据:
Student,
Id / integer
Name / String
City / String
Student 将是 table 的姓名,Id、Name 和 City 是列。我只需要如何解析 XML 文件并恢复数据。
您的 XML 文件是一个 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
模式集合,对其进行编译,然后查询由此定义的模式以获取您的主要元素名称及其属性。
假设您有一个字符串 xsdString
,其中包含问题中显示的 XSD。然后你可以加载它,编译它,打印出复杂的元素和它们的属性如下:
// 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);
}
}
}
}
输出
Complex element: Student
Element Id: Integer
Element Name: String
Element City: String
请注意,XSD 类型 xs:integer
对应于 无界 整数,不一定适合 Int32
或 Int64
.
参考文献:
-
How to parse an XSD to get the information from <xsd:simpleType> elements using C#?
XSD: What is the difference between xs:integer and xs:int?
演示 fiddle here.
我有这个通用 XML 文件,我想解析它以便根据 XML 数据创建一个 SQL 服务器 table :
<?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>
我想恢复以下数据:
Student,
Id / integer
Name / String
City / String
Student 将是 table 的姓名,Id、Name 和 City 是列。我只需要如何解析 XML 文件并恢复数据。
您的 XML 文件是一个 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
模式集合,对其进行编译,然后查询由此定义的模式以获取您的主要元素名称及其属性。
假设您有一个字符串 xsdString
,其中包含问题中显示的 XSD。然后你可以加载它,编译它,打印出复杂的元素和它们的属性如下:
// 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);
}
}
}
}
输出
Complex element: Student Element Id: Integer Element Name: String Element City: String
请注意,XSD 类型 xs:integer
对应于 无界 整数,不一定适合 Int32
或 Int64
.
参考文献:
How to parse an XSD to get the information from <xsd:simpleType> elements using C#?
XSD: What is the difference between xs:integer and xs:int?
演示 fiddle here.