xPath C#返回一个xml节点,该节点具有包含特定值的属性

本文关键字:节点 包含特 属性 xPath 返回 xml 一个 | 更新日期: 2023-09-27 18:11:33

我有要从中提取节点值的XML:

                            <oslc:creation rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/resources/com.ibm.rqm.planning.VersionedExecutionScript" />
                            <oslc:resourceShape rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/shape/creation/com.ibm.rqm.planning.VersionedExecutionScript" />
                            <oslc:resourceType rdf:resource="http://open-services.net/ns/qm#TestScript" />
                        </oslc:CreationFactory>
                    </oslc:creationFactory>
                    <oslc:creationFactory>
                        <oslc:CreationFactory>
                            <dcterms:title>Default creation factory for TestResult</dcterms:title>
                            <oslc:creation rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/resources/com.ibm.rqm.execution.ExecutionResult" />
                            <oslc:resourceShape rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/shape/creation/com.ibm.rqm.execution.ExecutionResult" />
                            <oslc:resourceType rdf:resource="http://open-services.net/ns/qm#TestResult" />
                        </oslc:CreationFactory>
                    </oslc:creationFactory>
                    <oslc:creationFactory>
                        <oslc:CreationFactory>
                            <dcterms:title>Default creation factory for TestCase</dcterms:title>
                            <oslc:creation rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/resources/com.ibm.rqm.planning.VersionedTestCase" />
                            <oslc:resourceShape rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/shape/creation/com.ibm.rqm.planning.VersionedTestCase" />
                            <oslc:resourceType rdf:resource="http://open-services.net/ns/qm#TestCase" />
                        </oslc:CreationFactory>
                    </oslc:creationFactory>
                    <oslc:creationFactory>
                        <oslc:CreationFactory>
                            <dcterms:title>Default creation factory for TestExecutionRecord</dcterms:title>
                            <oslc:creation rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/resources/com.ibm.rqm.execution.TestcaseExecutionRecord" />
                            <oslc:resourceShape rdf:resource="https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/shape/creation/com.ibm.rqm.execution.TestcaseExecutionRecord" />
                            <oslc:resourceType rdf:resource="http://open-services.net/ns/qm#TestExecutionRecord" />
                        </oslc:CreationFactory>
                    </oslc:creationFactory>
                    <oslc:creationFactory>
                        <oslc:CreationFactory>
                            <dcterms:title>Default creation factory for 
    </rdf:RDF>

我想要

<oslc:queryCapability>'<oslc:QueryCapability>' attribute value of oslc:queryBase (which is :"https://timo-pcvirtual:9443/qm/oslc_qm/contexts/_yAh_8gCIEeS7hY-fywlluw/resources/com.ibm.rqm.planning.VersionedTestCase")

我使用c#和这种格式,但不返回属性值。。。请帮忙。。。

    XPathNavigator nav = projectAreaContent.CreateNavigator();
    XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
    manager.AddNamespace("oslc", "http://open-services.net/ns/core#");
    manager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    XPathNodeIterator iterator = nav.Select("//oslc:queryBase[contains(@rdf:resource,'VersionedTestCase')]", manager);

xPath C#返回一个xml节点,该节点具有包含特定值的属性

使用Linq To Xml:

XNamespace oslc = "http://open-services.net/ns/core#";
XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
var list = XDocument.Parse(xmlstring)
          .Descendants(oslc + "QueryCapability")
          .Select(q => q.Element(oslc + "queryBase").Attribute(rdf + "resource").Value)
          .ToList();

您可以使用XDocument.Load(filename)从文件中加载xml。