c# - 最近的祖先的 xs :documentation node of an xs:element

标签 c# .net xml xpath wsdl

我有一个 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 节点获取文本“Structure for customer name”!

我已经尝试更改我在 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 相比,此函数(缺少正则表达式!)为用户提供了更好的线索,以了解导致验证失败的原因。

再次感谢。

最佳答案

@name 等于 "FullName"xs:element 元素没有 xs:documentation ancestor element ,而是一个preceding one.

所以,你可以使用:

//xs:element[@name='FullName']/preceding::xs:documentation[1]

注意:我使用了起始 // 运算符,因为我不知道您想深入到架构中有多深。

关于c# - 最近的祖先的 xs :documentation node of an xs:element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4853204/

相关文章:

c# - 在 C# 中,大写和小写字符串/字符串有什么区别?

c# - Rally SOAP API - 导入 .net 项目后缺少 RallyServiceService 类

.net - 在 VB.NET 中构建 REST 服务的教程?

c# - 如何在选定的打印机中打印任何文档

android - 更新图像的位置

c# - 如何从多个可选参数中指定参数

c# - 如何按存储在我的列表 <> 保存的结构中的整数对列表 <> 进行排序

c# - 需要帮助使用继承来接受有限状态机中的派生类型参数

java - Android studio gradle.build 应用程序午餐时出现问题

xml - Azure B2C 自定义策略注册 UI - 删除显示名称