3

XmlSchemaサブ要素の属性を取得するのに問題があります。

を拡張するとabstactElementがあります。ベースの属性の取得は、を使用して正常に機能します。ただし、を使用しての属性を取得することはできません。concreteElementabstractElementXmlSchemaComplexType.BaseXmlSchemaTypeconcreteElementXmlSchemaComplexType.Attributes

これは私の例のXml-Schemaファイルです:

<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name = "ConcreteElement" type="concreteElement" />

    <xs:complexType name="abstractElement">
        <xs:attribute name="aA1" type="xs:string" />
        <xs:attribute name="aA2" type="xs:string" />
        <xs:attribute name="aA3" type="xs:string" />    
    </xs:complexType>

    <xs:complexType name="concreteElement">
        <xs:complexContent>
            <xs:extension base="abstractElement">
                <xs:attribute name="cA1" type="xs:string"/>
                <xs:attribute name="cA2" type="xs:string"/>
                <xs:attribute name="cA3" type="xs:string"/>
            </xs:extension>    
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

ConcreteElement(cA1、cA2、cA3)のすべての属性と、その基本要素(aA1、aA2、aA3)のすべての属性を取得したいと思います。

私のコードは次のようになります。

public Program()
{
    XmlSchema xsd =
          XmlSchema.Read(
              new StreamReader("/Path/To/My/Xsd/File.xsd"),
              null);

    var xss = new XmlSchemaSet();
    xss.Add(xsd);
    xss.Compile();

    XmlSchemaElement xsdRoot = null;
    /* Get the root element */
    foreach (DictionaryEntry curEle in xsd.Elements)
    {
        var xse = (XmlSchemaElement)curEle.Value;
        xsdRoot = xse;
        break;
    }

    List<XmlSchemaAttribute> lsAttributes = this.GetAllAttributes(
                          xsdRoot.ElementSchemaType as XmlSchemaComplexType);
    foreach (XmlSchemaAttribute curAtr in lsAttributes)
    {
        Console.WriteLine(curAtr.Name);
    }

    Console.ReadKey();
}

そしてこれが私のGetAllAttributes方法です:

private List<XmlSchemaAttribute> GetAllAttributes(
    XmlSchemaComplexType comCon)
{
    /* No Ancestor, no Attributes */
    if (comCon == null)
    {
        return new List<XmlSchemaAttribute>();
    }

    /* Get attributs of the acestors */
    List<XmlSchemaAttribute> allAttributes =
        this.GetAllAttributes(
            comCon.BaseXmlSchemaType as XmlSchemaComplexType);

    /* Ad the attributes of the given element */
    allAttributes.AddRange(comCon.Attributes.Cast<XmlSchemaAttribute>());

    return allAttributes;
} 

よろしく、

4

1 に答える 1

2

最後に、解決策はAttributeUses、祖先に属するものも含め、要素のすべての属性を保持する Property を使用することです。

private List<XmlSchemaAttribute> GetAllAttributes(
    XmlSchemaComplexType comCon)
{        
    List<XmlSchemaAttribute> allAttributes = new List<XmlSchemaAttribute>();

    /* Add the attributes of the given element */
    foreach (DictionaryEntry curAttriEntry in comCon.AttributeUses)
    {
        XmlSchemaAttribute curAttri =
            curAttriEntry.Value as XmlSchemaAttribute;

        if (curAttri != null)
        {
            allAttributes.Add(curAttri);
        }
    }

    return allAttributes;
}
于 2012-07-09T09:09:38.253 に答える