在 C# 中访问 XSD 文件的元素 [英] accessing the elements of an XSD file in c#

查看:27
本文介绍了在 C# 中访问 XSD 文件的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了 S/O,但没有找到任何可以解决我的问题的东西(尽管 C# - 解析 XSD 架构 - 将所有元素组合框有点帮助).
在下面的 XSD 文件中,我需要获取嵌套元素的名称(SectionA,然后到 AX010_1、AX040_1.. 然后到 SectionB 和 BX010_1、BX070_1 等)

I have searched through S/O but not found anything to resolve my issue (though C# - Parsing XSD schema - get all elements to combobox has helped a little).
In the XSD file below, I need to get the name of the nested elements (SectionA and then to AX010_1, AX040_1.. then to SectionB and to BX010_1, BX070_1 etc)

如何访问嵌套元素?

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Vehicle">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Ford" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="SectionA" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="AX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX040_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX050_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX190_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="A080_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230F_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230G_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230GNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230H_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230HNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="AX230J_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="SectionB" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="BX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="BX070_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="BX350N_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="BX350NNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="SectionC" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="C010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="C010_2" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="C010_4" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="CF030_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="SectionD" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="D010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="D030_2" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                        <xs:element name="D560_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

推荐答案

在读取 XSD 文件时,需要考虑两件事:

When reading XSD files, there are two things to consider:

您需要在 XSD 文件中手动寻找:

You need to find your way manually in the XSD file:

  • 找到 complexType 节点
  • 找到序列
  • 找到元素
  • 等等

这并不难,但很乏味.如果您经常需要它,那么您可以将方法实现为扩展.下面的代码可以针对错误处理进行优化.

It is not difficult, but tedious. If you will need this often, than you could implement the methods as extensions. The code that follows could be optimised regarding the error handling.

使用 LINQ2XML 的可能解决方案如下所示:

A possible solution using LINQ2XML can look like this:

try
{           
    var xsd = XDocument.Load("d:\\temp\\input.xsd");
    var ns = xsd.Root.GetDefaultNamespace();
    var prefix = xsd.Root.GetNamespaceOfPrefix("xs");
    // get Vehicle
    var vehicle = xsd.Root.Element(prefix + "element");
    // get sequence for Ford
    var sections = vehicle.Element(prefix + "complexType")
                        .Element(prefix + "sequence")
                        // the Ford element
                        .Element(prefix + "element")
                        .Element(prefix + "complexType")
                        .Element(prefix + "sequence")
                        // elements
                        .Elements(prefix + "element").ToList();
    foreach (var section in sections)
    {
        Console.WriteLine(section.Attribute("name").Value);
        // for each section element
        var items = section.Element(prefix + "complexType")
                            .Element(prefix + "sequence")
                            // take the test elements
                            .Elements(prefix + "element");
        foreach (var item in items)
        {
            Console.WriteLine(item.Attribute("name").Value);
            // access another attributes or values here
        }
    }
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

输出为:

SectionA
AX010_1
AX040_1
AX050_1
AX190_1
A080_1
AX230F_1
AX230G_1
AX230GNOTE_1
AX230H_1
AX230HNOTE_1
AX230J_1
SectionB
BX010_1
BX070_1
...

这篇关于在 C# 中访问 XSD 文件的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆