在c#中访问XSD文件的元素

时间:2014-05-15 10:40:04

标签: c# xml xsd

我搜索过S / O但找不到任何可以解决我的问题的方法(虽然C# - Parsing XSD schema - get all elements to combobox已经帮了一点)。
在下面的XSD文件中,我需要获取嵌套元素的名称(SectionA然后到AX010_1,AX040_1 ..然后到SectionB和BX010_1,BX070_1等)

如何访问嵌套元素?

<?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>

2 个答案:

答案 0 :(得分:2)

xsd只不过是带有命名空间的

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\myXsd.xsd");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

XmlNodeList nodes =
    xmlDoc.SelectNodes("//xs:element[starts-with(@name, 'Section')]/*//xs:element", nsmgr);

List<string> namesList = new List<string>();

foreach (XmlNode node in nodes)
{
    namesList.Add(node.Attributes["name"].Value);
}

应该产生所需的输出。

答案 1 :(得分:2)

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

您需要在XSD文件中手动找到您的方式

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

这并不难,但很乏味。如果您经常需要这个,那么您可以将这些方法实现为扩展。可以针对错误处理优化随后的代码。

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

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
...