Re: User-defined attributes in XMLSchema



..NET XmlSchema Object Model, the search:able attribute will be stored in the
element's UnhandledAttributes array.
While validating an xml file against the schema in .NET 2.0, you can access
the schema information through the SchemaInfo property on the reader as
shown below in the code sample:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schemaSample.xsd");

XmlReader validatingReader = XmlReader.Create("myxml.xml", settings);
while(validatingReader.Read()) {
if (validatingReader.NodeType == Element) {
IsSearchableElement(validatingReader.SchemaInfo.SchemaElement) {
//do search:able processing here
}
}
}

private bool IsSearchableElement(XmlSchemaAnnotated annotatedSchemaObject) {
if (annotatedSchemaObject.UnhandledAttributes != null) {
foreach(XmlAttribute att in annotated.UnhandledAttributes) {
if (att.LocalName == "able" && att.NamespaceURI == "SearchNs") {
return true;
}
}
}
return false;
}

Thanks,
Priya

"Geir Aamodt" <geir_aamodt@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D5B582EE-515A-4B78-9493-75114560BB8A@xxxxxxxxxxxxxxxx
I have an xml schema and an xml file. When parsing the xml file I would
like
to perform operations on elements marked in the schema with the
"search:able"
attribute. See sample files below.

Any tips/tricks on how to achieve this?

----------------------XML Schema sample----------------------
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:search="http://www.test.com/test";>
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomerID" type="xs:unsignedByte" search:able="ID"/>
<xs:element name="FirstName" type="xs:string" search:able="Y"/>
<xs:element name="LastName" type="xs:string" search:able="N"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

----------------------XML File sample----------------------
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<CustomerID>1</CustomerID>
<FirstName>Geir</FirstName>
<LastName>Aamodt</LastName>
</Customer>
<Customer>
<CustomerID>2</CustomerID>
<FirstName>Test</FirstName>
<LastName>Testing</LastName>
</Customer>
</Customers>


.



Relevant Pages


Loading