c# – xs:element的最近祖先的xs:documentation节点
发布时间:2020-12-16 01:40:52 所属栏目:百科 来源:网络整理
导读:我有一个wsdl文档,其摘录如下所示…… xs:complexType name="CustomerNameType" xs:annotation xs:documentationStructure for customer name/xs:documentation /xs:annotation xs:sequence xs:element name="FullName" minOccurs="0" xs:simpleType xs:rest
我有一个wsdl文档,其摘录如下所示……
<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> 我知道xs:element / @ name,我想得到最近的xs:documentation元素. 使用上面的示例,我知道xs:element / @ name =“FullName”,我想从最近的xs:documentation节点获取文本“客户名称的结构”! 我已经尝试更改我在stackoverflow(和其他站点)上找到的一些示例,但它们都不起作用.典型:0). 干杯. 感谢您回答…希望这会有用…… 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 = sElementName.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(); } 基本上,sOriginalErrorMessage =一个XmlSchemaException.ToString(),而Properties.Resources.ServiceRequest_Service_74b1是验证数据的wsdl.这个函数(缺少正则表达式!)为用户提供了更好的线索,告知导致验证失败的原因.与旧的XmlSchemaException相反. 再次感谢. 解决方法
以下XPATH将返回指定元素和所有父文档的文档.我认为亚历杭德罗的回答可能会返回一个兄弟的文档,在不同的模式中你可能不太感兴趣.
(//xs:element[@name='orderRequest'] /ancestor-or-self::* /xs:annotation /xs:documentation)[last()] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |