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;
}