从XMLSchema中获取列名和类型

时间:2018-12-10 20:34:13

标签: c# xml .net-core xml-parsing xml-schema-collection

我正在用C#工作,试图从xml模式中提取列名及其类型。这是我收到的架构:

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="WbDT" msdata:UseCurrentLocale="true">
<xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="WbDT">
        <xs:complexType>
        <xs:sequence>
            <xs:element name="TranslateNo" type="xs:int" minOccurs="0" />
            <xs:element name="EngWord" type="xs:string" minOccurs="0" />
            <xs:element name="LanguageCd" type="xs:string" minOccurs="0" />
            <xs:element name="TranslateWord" type="xs:string" minOccurs="0" />
            <xs:element name="UpdateDTS" type="xs:dateTime" minOccurs="0" />
            <xs:element name="UpdateBy" type="xs:string" minOccurs="0" />
        </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:choice>
</xs:complexType>
</xs:element>

我想从中得到的是一个Dictionary,其中每个条目都是“ ColumnName”,“ ColumnType”。

虽然我有点不高兴使用Synatax,但到目前为止,这是我所拥有的:

var xml = result.WbDT;
XmlSchemaSet schemaSet = new XmlSchemaSet();
string xmlheader = "<?xml version='1.0' encoding='utf-8'?>" + System.Environment.NewLine;
string allxml = xmlheader + xml.Any[0].ToString();
schemaSet.Add("", XmlReader.Create(new StringReader(allxml)));
schemaSet.Compile();

XmlSchema customerSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
    customerSchema = schema;
}

foreach (XmlSchemaElement element in customerSchema.Elements.Values)
{
    Console.WriteLine("Element: {0}", element.Name);

到目前为止,我现在有一个包含1个元素的架构。.现在我需要做的是深入研究-> -> < xs:sequence>元素并对其进行迭代以获取列名称及其类型。

到目前为止,我正在使用的语法是:

XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;
XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;

//// Iterate over each XmlSchemaElement in the Items collection.
foreach (XmlSchemaElement childElement in sequence.Items)
{
    Console.WriteLine("Element: {0}", childElement.Name);
}

但是当我尝试运行这段代码时,我在sequence.items上得到了一个空异常

我正在尝试遵循以下Microsoft文档:https://docs.microsoft.com/en-us/dotnet/standard/data/xml/traversing-xml-schemas

我很确定问题是我的xml比示例中的xml具有更深的结构

示例xml

<xs:element name="Customer">  
    <xs:complexType>  
        <xs:sequence>  
            <xs:element name="FirstName" type="xs:string" />

我的xml

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="WbDT" msdata:UseCurrentLocale="true">
<xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="WbDT">
        <xs:complexType>
        <xs:sequence>
            <xs:element name="TranslateNo" type="xs:int" minOccurs="0" />

2 个答案:

答案 0 :(得分:1)

您的问题确实是您的XML的层次结构比下面的示例还要深。您只需添加一些额外的步骤即可解决该问题:

foreach (XmlSchemaElement element in customerSchema.Elements.Values)
{
    Console.WriteLine("Element: {0}", element.Name);
    XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;
    XmlSchemaChoice choice = complexType.ContentTypeParticle as XmlSchemaChoice;
    XmlSchemaElement outerElement = choice.Items.Cast<XmlSchemaElement>().First();
    XmlSchemaComplexType innerComplexType = outerElement.ElementSchemaType as XmlSchemaComplexType;
    XmlSchemaSequence xmlSchemaSequence = innerComplexType.ContentTypeParticle as XmlSchemaSequence;

    //// Iterate over each XmlSchemaElement in the Items collection.
    foreach (XmlSchemaElement childElement in xmlSchemaSequence.Items)
    {
        Console.WriteLine("Element: {0}", childElement.Name);
    }
}

答案 1 :(得分:0)

使用.net framework 4.7.2并命名空间System.Xml.Schema

另一个选项

XmlDocument xml =新的XmlDocument();             xml.LoadXml(xmlString);

        XmlNodeList xnList = xml1.SelectNodes("/root/table[@name='Role']");
        foreach (XmlNode xn in xnList)
        {
            string colName = xn["column"].InnerText;

        }