0

I have an XSD document, and I need to select all nodes that match a certain layout.

Here's a snippet of the XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="MachineParameters">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="stMachineParameters"
         minOccurs="1"
         maxOccurs="1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="CPSPEED"
                     minOccurs="1"
             maxOccurs="1">
                  <xsd:annotation>
        <xsd:documentation>CPSPEEDDesc</xsd:documentation>
        <xsd:appinfo>false</xsd:appinfo>
          </xsd:annotation>
          <xsd:simpleType>
                    <xsd:restriction base = "xsd:decimal">
                    </xsd:restriction>
                  </xsd:simpleType>
                </xsd:element>  
                <xsd:element name="STVARZPARAMS"
                     minOccurs="1"
                             maxOccurs="1">
                  <xsd:complexType>
                    <xsd:sequence>
                      <xsd:element name="VARIABLEZFASTVELOCITY">
                        <xsd:annotation>
                          <xsd:documentation>VARIABLEZFASTVELOCITYDesc</xsd:documentation>
                          <xsd:appinfo>false</xsd:appinfo>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base = "xsd:decimal">
                            <xsd:minInclusive value="0" />
                            <xsd:maxInclusive value="1" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:element>

And so on.

I am trying to write some C# code to run through my entire document and return me a list of any element where an xsd:appinfo has been specified, regardless of value.

I've been struggling with this for a while and feel like I am close, but so far I haven't hit on the right Xpath query (I haven't used it before).

Here's the C#:

elementInfo = new Dictionary<string, DictionaryInfo>();

XmlNodeList nodeList;
XmlNode root = xmlDocSchema.DocumentElement;


try
{
    // the presence of an annotation/appinfo for the element is being used to identify it as a value element
    XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocSchema.NameTable);
    xmlNamespaceManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
    nodeList = root.SelectNodes("/*/element[/annotation/appinfo='false' or /annotation/appinfo='true']", xmlNamespaceManager);
}
catch (System.Xml.XPath.XPathException ex)
{
    MessageBox.Show(string.Format("Xpath exception: {0}", ex.Message));
    nodeList = null;
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("General exception: {0}", ex.Message));
    nodeList = null;
}

Could someone suggest where I'm going wrong (and how to go right!)?

1

1 Answer 1

1

I think you want to use

"//xsd:element[xsd:annotation/xsd:appinfo]"

as your xpath. There are a couple of changes to what you were using:

  • //element is the syntax for selecting an element at any level in the document. /*/element selects only elements that are children of the root node.

  • You need to use the namespace prefix on every element using that namespace in your XPath.

  • There's no need to check the value of a predicate if you're not interested in it; just specifying an element name (or path) checks for existence.

  • Starting a predicate with / is rarely what you want. It ignores the current context, and tries to match the predicate starting at the root of the document (in your case, the predicate [/annotation/appinfo] is only true if the root-node is an annotation element, with an appinfo child.)

2
  • Thanks. That answered the question perfectly, with extra info to guide me in the future. I don't suppose you also know how I can access the parent nodes of elements once they have been put into the list by SelectNodes? Aug 12, 2013 at 7:26
  • Sadly, I'm not really a c# guy, so I can't help you with its specific APIs; I'm sure if you open a new question about that, someone will be able to help out though. Aug 12, 2013 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.