微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – xs:element的最近祖先的xs:documentation节点

我有一个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()]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐