1

我在 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>

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

不过,我在语法方面有点挣扎,这是我到目前为止所拥有的:

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:complexType> -> <xs:element name="WbDT"> -> <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

<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" />
4

2 回答 2

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);
    }
}
于 2018-12-10T21:42:30.040 回答
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;

        }
于 2018-12-10T21:14:42.150 回答