tags:

views:

192

answers:

3

Hi,

I am trying to read enum from a XSD file. The schema is as follows

 <xs:schema id="v1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="unqualified" elementFormDefault="qualified" msdata:IsDataSet="true">   
<xs:simpleType name="Type">
     <xs:restriction base="xs:string">
      <xs:enumeration value="Op1" />
      <xs:enumeration value="Op2" />
      <xs:enumeration value="Op3" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

I also tried using this but I am getting list item count as Zero. Following is the code I am using

DataSet _sR = new DataSet();
 _sR.ReadXmlSchema(assembly.GetManifestResourceStream("v1.xsd"));
    XmlDocument xDoc = new XmlDocument();
             xDoc.LoadXml(_sR.GetXml());
             XmlNamespaceManager xMan = new XmlNamespaceManager(xDoc.NameTable);
             xMan.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

             XmlNodeList xNodeList = xDoc.SelectNodes(
                            "//xs:schema/xs:simpleType[@name='Type']/xs:restriction/xs:enumeration", xMan);

             string[] enumVal = new string[xNodeList.Count];
             int ctr = 0;
             foreach (XmlNode xNode in xNodeList)
             {
                enumVal[ctr] = xNode.Attributes["value"].Value;
                ctr++;
             }
+1  A: 

You can get the enumerated values by using the XmlSchema object which provides programmatic access all the elements of the xsd.

using System.Xml;
using System.Xml.Schema;


XmlSchema schema = XmlSchema.Read(XmlReader.Create("v1.xsd"), 
    new ValidationEventHandler(ValidationCallbackOne));
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(schema);
schemaSet.Compile();

IEnumerable<string> enumeratedValues = schema.Items.OfType<XmlSchemaSimpleType>()
    .Where(s => (s.Content is XmlSchemaSimpleTypeRestriction) 
        && s.Name == "Type")
    .SelectMany<XmlSchemaSimpleType, string>
        (c =>((XmlSchemaSimpleTypeRestriction)c.Content)
            .Facets.OfType<XmlSchemaEnumerationFacet>().Select(d=>d.Value));

// will output Op1, Op2, Op3...
foreach (string s in enumeratedValues)
{
    Console.WriteLine(s);
}
code4life
A: 

Here is a way to do it using XLinq, which IMO is much simpler:

XDocument xDoc = XDocument.Load(assembly.GetManifestResourceStream("v1.xsd"));
XNamespace xs = "http://www.w3.org/2001/XMLSchema";

var enums = xDoc.Descendants(xs + "enumeration");
Console.WriteLine(enums.Count()); // Tested, outputs 3.
Domenic
A: 

The problem, I discovered, is because of your intermediate DataSet step. That is, the following code works:

var xDoc = new XmlDocument();
xDoc.Load(assembly.GetManifestResourceStream("v1.xsd"));

var xMan = new XmlNamespaceManager(xDoc.NameTable);
xMan.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

var xNodeList = xDoc.SelectNodes("//xs:schema/xs:simpleType[@name='Type']/xs:restriction/xs:enumeration", xMan);
Console.WriteLine(xNodeList.Count);

But when I try to use your intermediate DataSet, with ReadXmlSchema and LoadXml from the DataSet's GetXml, it does not work.

Domenic