简体   繁体   中英

C# Xpath node finding elements

I need some help with my code. I'm finding at an XML document like this:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 

xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/Prueba" targetNamespace="urn:microsoft-dynamics-schemas/codeunit/Prueba">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-schemas/codeunit/Prueba">
<element name="FuncionPublica">
<complexType>
<sequence/>
</complexType>
</element>
<element name="FuncionPublica_Result">
<complexType>
<sequence/>
</complexType>
</element>
<element name="FuncionParam">
<complexType>
<sequence>
<element minOccurs="1" maxOccurs="1" name="parametro1" type="int"/>
</sequence>
</complexType>
</element>
<element name="FuncionParam_Result">
<complexType>
<sequence/>
</complexType>
</element>
<element name="FuncionVar">
<complexType>
<sequence>
<element minOccurs="1" maxOccurs="1" name="num" type="int"/>
</sequence>
</complexType>
</element>
<element name="FuncionVar_Result">
<complexType>
<sequence>
<element minOccurs="1" maxOccurs="1" name="num" type="int"/>
</sequence>
</complexType>
</element>
<element name="FuncionReturn">
<complexType>
<sequence/>
</complexType>
</element>
<element name="FuncionReturn_Result">
<complexType>
<sequence>
<element minOccurs="1" maxOccurs="1" name="return_value" type="int"/>
</sequence>
</complexType>
</element>
</schema>
</types>
<message name="FuncionPublica">
<part name="parameters" element="tns:FuncionPublica"/>
</message>
<message name="FuncionPublica_Result">
<part name="parameters" element="tns:FuncionPublica_Result"/>
</message>
<message name="FuncionParam">
<part name="parameters" element="tns:FuncionParam"/>
</message>
<message name="FuncionParam_Result">
<part name="parameters" element="tns:FuncionParam_Result"/>
</message>
<message name="FuncionVar">
<part name="parameters" element="tns:FuncionVar"/>
</message>
<message name="FuncionVar_Result">
<part name="parameters" element="tns:FuncionVar_Result"/>
</message>
<message name="FuncionReturn">
<part name="parameters" element="tns:FuncionReturn"/>
</message>
<message name="FuncionReturn_Result">
<part name="parameters" element="tns:FuncionReturn_Result"/>
</message>
<portType name="Prueba_Port">
<operation name="FuncionPublica">
<input name="FuncionPublica" message="tns:FuncionPublica"/>
<output name="FuncionPublica_Result" message="tns:FuncionPublica_Result"/>
</operation>
<operation name="FuncionParam">
<input name="FuncionParam" message="tns:FuncionParam"/>
<output name="FuncionParam_Result" message="tns:FuncionParam_Result"/>
</operation>
<operation name="FuncionVar">
<input name="FuncionVar" message="tns:FuncionVar"/>
<output name="FuncionVar_Result" message="tns:FuncionVar_Result"/>
</operation>
<operation name="FuncionReturn">
<input name="FuncionReturn" message="tns:FuncionReturn"/>
<output name="FuncionReturn_Result" message="tns:FuncionReturn_Result"/>
</operation>
</portType>
<binding name="Prueba_Binding" type="tns:Prueba_Port">
<binding xmlns="http://schemas.xmlsoap.org/wsdl/soap/" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="FuncionPublica">
<operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="urn:microsoft-dynamics-schemas/codeunit/Prueba:FuncionPublica" style="document"/>
<input name="FuncionPublica">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</input>
<output name="FuncionPublica_Result">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</output>
</operation>
<operation name="FuncionParam">
<operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="urn:microsoft-dynamics-schemas/codeunit/Prueba:FuncionParam" style="document"/>
<input name="FuncionParam">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</input>
<output name="FuncionParam_Result">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</output>
</operation>
<operation name="FuncionVar">
<operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="urn:microsoft-dynamics-schemas/codeunit/Prueba:FuncionVar" style="document"/>
<input name="FuncionVar">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</input>
<output name="FuncionVar_Result">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</output>
</operation>
<operation name="FuncionReturn">
<operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="urn:microsoft-dynamics-schemas/codeunit/Prueba:FuncionReturn" style="document"/>
<input name="FuncionReturn">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</input>
<output name="FuncionReturn_Result">
<body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
</output>
</operation>
</binding>
<service name="Prueba">
<port name="Prueba_Port" binding="tns:Prueba_Binding">
<address xmlns="http://schemas.xmlsoap.org/wsdl/soap/" location="http://pcnav02.datanet.local:7017/DynamicsNAV100/WS/CRONUS%20Espa%C3%B1a%20S.A./Codeunit/Prueba"/>
</port>
</service>
</definitions>

So, I need to get the value parametro1 that is inside element with name="FuncionParam"and then located at sequence/complexType/element

foreach (XmlNode node in docXML.GetElementsByTagName("sequence"))
{
    XmlNodeList lstElements = node.ChildNodes;
    for (int j = 0; j < lstElements.Count; j++)
    {
        Console.WriteLine("{0}", node.InnerXml);
    }
}

The above code shows me the following result:

<element minOccurs="1" maxOccurs="1" name="parametro1" type="int" 
xmlns="http://www.w3.org/2001/XMLSchema" />

I need to get that value, parametro1 but first I need to search by the name= "FuncionParam" .

Can you check for the GetElementsByXpath

Use below xpath :-

//element[@name='FuncionParam']//sequence

It will return :-

Element=<sequence xmlns="http://www.w3.org/2001/XMLSchema">
                     <element maxOccurs="1" minOccurs="1" name="parametro1" type="int"/>
                 </sequence>

Hope it will help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM