Read a value between two labels in a string

0

I am receiving an xml in the form of a string Example:

POST xml.xml HTTP/1.1
Content-Type: application/xml
Content-Length: 280

<?xml version="1.0" encoding="ISO-8859-1" ?>
<NODO1>
    <PARAM1>VALOR1</PARAM1>
    <PARAM2>VALOR2</PARAM2>
    <PARAM3>VALOR3</PARAM3>
    <PARAM4>VALOR4</PARAM4>
    <PARAM5>VALOR5</PARAM5>
    <PARAM6>VALOR6</PARAM6>
    <PARAM7>VALOR7</PARAM7> 
    <PARAM8>VALOR8</PARAM8>
</NODO1>

I need to know if it is empty between the opening and closing of NODO1 Example:

POST xml.xml HTTP/1.1Content-Type: application/xmlContent-Length: 280<?xml version="1.0" encoding="ISO-8859-1" ?><NODO1>Aca</NODO1>

Thanks greetings.

    
asked by Dario Nicolas Orazi 20.03.2018 в 20:24
source

2 answers

2

If what you want is to see the content between the labels, you could start by obtaining the position of < NODO1 > and < / NODO1 > within the text received as follows: Assuming that the text is in the variable 'text' and this is of the string type

var posicionInicial = texto.indexOf("<NODO1>") + 7;
var posicionFinal = texto.indexOf("</NODO1>";

Now it remains to validate if the initial position and the final position are different, if they are different it is because there is some text between these two labels. If there is a text and you want to extract it, you could do it with:

var textoNodo1 = texto.Substring(posicionInicial, (posicionFinal - posicionInicial));
    
answered by 20.03.2018 / 21:08
source
1

I would do the following .. First I converted your string to a XmlDocument

    string a = "<NODO1>" +
    "<PARAM1> <b>VALOR1</b> </PARAM1>" +
    "<PARAM2> VALOR2 </PARAM2>" +
    "<PARAM3> VALOR3 </PARAM3>" +
    "<PARAM4> VALOR4 </PARAM4>" +
    "<PARAM5> VALOR5 </PARAM5>" +
    "<PARAM6> VALOR6 </PARAM6>" +
    "<PARAM7> VALOR7 </PARAM7>" +
    "<PARAM8> VALOR8 </PARAM8>" +
    "</NODO1>";

    //Converto a Xml Document
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(a);

    //Pregunto por el NODO 1
    if (xml.SelectSingleNode("NODO1") != null){MessageBox.Show("Existe");}
    else{ MessageBox.Show("No Existe!");}

    //Pregunto por PARAM1
    if (xml.SelectSingleNode("//PARAM1") != null){MessageBox.Show("Existe");}
    else{ MessageBox.Show("No Existe!");}

I hope you serve, greetings!

    
answered by 20.03.2018 в 20:58