Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取子元素的所有属性_C#_Xsd - Fatal编程技术网

C# 获取子元素的所有属性

C# 获取子元素的所有属性,c#,xsd,C#,Xsd,我在获取XmlSchema子元素的属性时遇到问题 有一个abstractElement和一个concreteElement扩展了abstractElement。 使用XmlSchemaComplexType.BaseXmlSchemaType可以很好地获取基的属性。 但是使用XmlSchemaComplexType.attributes获取concreteElement的属性不起作用 这是我的示例Xml架构文件: <xs:schema id="XMLSchema1" targetN

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

有一个
abstractElement
和一个
concreteElement
扩展了
abstractElement
。 使用
XmlSchemaComplexType.BaseXmlSchemaType
可以很好地获取基的属性。 但是使用
XmlSchemaComplexType.attributes
获取
concreteElement
的属性不起作用

这是我的示例Xml架构文件:

<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>
这是我的
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;
} 
私有列表GetAllAttribute(
XmlSchemaComplexType(comCon)
{
/*没有祖先,就没有属性*/
如果(comCon==null)
{
返回新列表();
}
/*获取Acestor的属性*/
列出所有属性=
这个.GetAllAttributes(
comCon.BaseXmlSchemaType为XmlSchemaComplexType);
/*指定元素的属性*/
AddRange(comCon.Attributes.Cast());
返回所有属性;
} 

关于,

最后,解决方案是使用属性
attributeUse
,它保存所有属性 指一种元素,甚至是那些属于祖先的元素

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;
}
私有列表GetAllAttribute(
XmlSchemaComplexType(comCon)
{        
List allAttributes=新列表();
/*添加给定元素的属性*/
foreach(comCon.AttributeUse中的字典入门课程)
{
XmlSchemaAttribute curAttri=
值作为XmlSchemaAttribute;
if(curAttri!=null)
{
添加所有属性(curAttri);
}
}
返回所有属性;
}
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;
}