Parsing Xsd for attributes in C #

I am trying to parse a custom XSD to create a list of elements that define custom attributes. I have pasted a sample node from my XSD below. In my case, I am trying to create a list of all elements (simple and complex types) that have been marked as static. Sample -

    <xs:element name="ATestEnum">
      <xs:annotation>
        <xs:appinfo>
          <ConfigurationMemberMetadata>
            <Static>False</Static>
          </ConfigurationMemberMetadata>
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="Sample1" />
          <xs:enumeration value="Sample2" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>

      

I just can't seem to find a way to query the "inside" element for the properties I want. I got to this -

var doc = XDocument.Load(schemaFileName);            
foreach (var element in doc.Descendants(xs + "element"))
{
    Console.WriteLine(element.Attribute("name").Value);
}

      

This gives me a list of all the items in the xsd, but not the specific property (ConfigurationMemberMetadata) that I need to check. I can see the element.Nnotation (type) method, but how do I drop it to retrieve the fields I'm looking for?

I also tried using XmlTextReader and read the schema, compile the schema and iterate through the elements, but that doesn't help either.

Can anyone point me in the right direction? I am very new to parsing xml and will greatly appreciate your help! Thank.

+3


source to share


2 answers


You should use something like this:

XmlReader reader = XmlReader.Create(@"D:\....\your-file.xsd");
XDocument doc = XDocument.Load(reader);
XmlNamespaceManager ns = new XmlNamespaceManager(reader.NameTable);
ns.AddNamespace("", "http://tempuri.org/XMLSchema.xsd");
XNamespace xs = "http://www.w3.org/2001/XMLSchema";
foreach (var element in doc.Descendants(xs + "element")) { Console.WriteLine(element.Attribute("name").Value); }
foreach (XElement element in (IEnumerable)doc.XPathEvaluate("//ConfigurationMemberMetadata")) { Console.WriteLine(element.Name); }

      

Annotations are not displayed in the info set, that's something else. Use XPath to navigate to your element.



One thing to look out for is the namespace you associate with your prefix. Typically, in the XSD file, the default namespace matches the target namespace, so I set up a dummy namespace - update to match yours. If you don't have a default namespace, just set the uri to an empty string.

Here is the XSD I used with the above code:

<?xml version="1.0" encoding="utf-8" ?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="ATestEnum"> 
        <xs:annotation> 
            <xs:appinfo> 
                <ConfigurationMemberMetadata> 
                    <Static>False</Static> 
                </ConfigurationMemberMetadata> 
            </xs:appinfo> 
        </xs:annotation> 
        <xs:simpleType> 
            <xs:restriction base="xs:string"> 
                <xs:enumeration value="Sample1"/> 
                <xs:enumeration value="Sample2"/> 
            </xs:restriction> 
        </xs:simpleType> 
    </xs:element>
</xs:schema>

      

+4


source


Use the Xpath query to selectively find only the nodes you are interested in ("static" nodes).

Here's a good example of how to do it: http://support.microsoft.com/kb/308333

If you need XPath practice, use this "testbed" to familiarize yourself with its syntax: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm



Using your XML sample, this Xpath query will find all the "static" nodes:

/xml/xs:element/xs:annotation/xs:appinfo/ConfigurationMemberMetadata/Static

      

+2


source







All Articles