Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I've learned a lot about XSD and XML from your site. Thanks for all the information that you put there. I have a question below.

I've searched a lot of sites and pages and could not find a way to accomplish the below.

I am writing a C# program and in the program I need to read an XSD file and get the "type=" value for each element. For example I might have an element such as below:

XML
<xs:element name="Amount" minoccurs="0" maxoccurs="1" type="xs:date" xmlns:xs="#unknown">



I'd like to get the "xs:date" to save in a string variable. I can traverse the XSD file and get all element names but I simply cannot find a way to get to the type.

Can you please provide some help?

Thank You!
Posted
Updated 12-Feb-13 11:06am
v2
Comments
Sandeep Mewara 12-Feb-13 14:22pm    
Are you directing this question to someone specific? We are missing the context and not too sure what exactly you are referring to... an article?
Member 9740382 12-Feb-13 15:44pm    
If anyone knows the answer, please post. Much appreciation! Here is what I am trying to do. In my XSD file I have several elements and one is:
Member 9740382 13-Feb-13 9:02am    
Thanks for your reply! I already have a way of reading the schema and getting at all elements. The one thing I am looking to do is to extract the element type. For example I have a line below:
" <xs:element name="Quantity" minoccurs="0" maxoccurs="1" type="xs:integer"> "
I know how to read the above to get the name, minOccurs, and maxOccurs. What I don't know is how to get the type. I'd like to read the element type to return 'xs:integer'.
Any help would be greatly appreciated!

1 solution

key points are:

1) XSD file itself xml document.

2) xml files can be queried with xpath - in C# it could be done with SelectNodes method of xml doc.
C#
SelectNodes("//somenode", nsmgr);


3)As you see SelectNodes has nsmgr parameter. (XmlNamespaceManager nsmgr)
If you want to query xsd which is in xs: namespace, you have to add it:

C#
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

4) dirty xpath to get all xs:elements with type attributes set is:

XML
//xs:element[@type]



http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectnodes.aspx[^]

http://msdn.microsoft.com/ru-ru/library/system.xml.xmlnamespacemanager.addnamespace(v=vs.95).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900