获取子元素的所有属性

3

我在获取XmlSchema的子元素属性时遇到了问题。

有一个abstactElement和一个扩展abstractElementconcreteElement。 使用XmlSchemaComplexType.BaseXmlSchemaType可以很好地获取基类的属性。 但是使用XmlSchemaComplexType.Attributes获取concreteElement的属性无法正常工作。

这是我的示例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;
} 

敬礼,

1个回答

2
最终的解决方案是使用属性AttributeUses,它包含了元素的所有属性,甚至包括祖先元素的属性。
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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接