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#写这个xml

c# - 运行 "dotnet run ..."后 PowerShell 不返回命令行

c# - 自定义数据注释(数据类型)

java - 无论尺寸如何,如何使图像适合屏幕?

c# - 访问 Linq 查询的基本列表

c# - Windows 窗体 WebBrowser 控件和 iframe

c# - 如果 XSD 未指定 maxOccurs,则 XML 文档中的最大元素数是多少?

c# - 查找满足某些条件的所有集合( vector )

c# - 为什么我得到 Ice::MemoryLimitException,即使是 Ice.MessageSizeMax=2000000

c# - System.IO.File.Delete 抛出算术运算导致溢出