I have a wsdl document, an extract of which is shown below...
<xs:complexType name="CustomerNameType">
<xs:annotation>
<xs:documentation>Structure for customer name</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="FullName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Forenames" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
i know the xs:element/@name and i would like to get the nearest xs:documentation element.
Using the example above, i know that xs:element/@name = "FullName", and i would like to get the text "Structure for customer name" from the nearest xs:documentation node!
I have tried changing a few examples that i have found on stackoverflow (and other sites), but none of them work. Typical :0).
Cheers.
Thanks for answering guys ... hopefully this will come in useful ...
public static string DecryptStupidCapsError(string sOriginalErrorMessage)
{
string sProblem = sOriginalErrorMessage.Substring(sOriginalErrorMessage.IndexOf("---> System.Xml.Schema.XmlSchemaException: ") + "---> System.Xml.Schema.XmlSchemaException: ".Length);
sProblem = sProblem.Substring(0, sProblem.IndexOf("An error occurred"));
string sElementName = sProblem.Substring(sProblem.IndexOf(":") + 1);
sElementName = sElementN开发者_开发知识库ame.Substring(sElementName.IndexOf(":") + 1);
sElementName = sElementName.Substring(0, sElementName.IndexOf("'"));
XmlDocument xd = new XmlDocument();
xd.LoadXml(Properties.Resources.ServiceRequest_Service_74b1);
XmlNamespaceManager xnsm = new XmlNamespaceManager(xd.NameTable);
XPathDocument x = new XPathDocument(new StringReader(Properties.Resources.ServiceRequest_Service_74b1));
XPathNavigator foo = x.CreateNavigator();
foo.MoveToFollowing(XPathNodeType.Element);
IDictionary<string, string> whatever = foo.GetNamespacesInScope(XmlNamespaceScope.All);
foreach (KeyValuePair<string, string> xns in whatever)
{
xnsm.AddNamespace(xns.Key, xns.Value);
}
XmlNodeList xnl = xd.SelectNodes("//xs:element[@name='" + sElementName + "']", xnsm);
StringBuilder sb = new StringBuilder();
sb.AppendLine("CAPS has reported a (cryptic) error whilst validating the data you entered.");
sb.AppendLine();
sb.AppendLine("The following summary should enable you to determine what has caused the '" + sElementName + "' data to be invalid.");
sb.AppendLine("----------");
string sLast = string.Empty;
foreach (XmlElement xe in xnl)
{
StringBuilder sbLast = new StringBuilder();
XmlElement xeDocumentation = (XmlElement)xe.OwnerDocument.SelectSingleNode("(//xs:element[@name='" + sElementName + "']/ancestor-or-self::*/xs:annotation/xs:documentation)[last()]", xnsm);
if (xeDocumentation.InnerText == sLast) continue;
sbLast.AppendLine(sElementName + " AKA " + xeDocumentation.InnerText + ": ");
sbLast.AppendLine("has the following validation rules:");
XDocument xdoc = XDocument.Parse(xe.OuterXml);
sbLast.AppendLine(xdoc.ToString());
sbLast.AppendLine("----------");
sb.AppendLine(sbLast.ToString());
sLast = xeDocumentation.InnerText;
}
return sb.ToString();
}
Basically, sOriginalErrorMessage = an XmlSchemaException.ToString(), and Properties.Resources.ServiceRequest_Service_74b1 is the wsdl that the data was validated against. This function (that is lacking regex!) gives much better clues to a user as to what has caused a validation failure., as opposed to the old XmlSchemaException.
Thanks again.
The xs:element
element with @name
equal to "FullName"
hasn't an xs:documentation
ancestor element , but a preceding one.
So, you could use:
//xs:element[@name='FullName']/preceding::xs:documentation[1]
Note: I have used an starting //
operator because I don't known how deep into the schema you want to go.
The following XPATH will return the documentation of the specified element and all parent's documentation. I think Alejandro's answer may return a sibling's documentation which may be of less interest to you in a different schema.
(//xs:element[@name='orderRequest']
/ancestor-or-self::*
/xs:annotation
/xs:documentation)[last()]
精彩评论