none
Load list node - XML RRS feed

  • Pergunta

  • Hi

    My file XML:

    ...
    <SalesInvoices>
    	<NumberOfEntries>90</NumberOfEntries>
    	<TotalDebit>100.00</TotalDebit>
    	<TotalCredit>50000.00</TotalCredit>
       	
    	<Invoice>
    		<InvoiceNo>FT D0/1962</InvoiceNo>
       		<InvoiceDate>2015-04-01</InvoiceDate>
            <InvoiceType>FT</InvoiceType>
          	<CustomerID>52</CustomerID>
            <Line>
    			<LineNumber>1</LineNumber>
            	<ProductCode>2060075838680</ProductCode>
            	<ProductDescription>PR CARV AMERIC</ProductDescription>
    			<Quantity>1</Quantity>
    			<UnitPrice>78.50</UnitPrice>
            </Line>
    		<Line>
    			<LineNumber>2</LineNumber>
            	<ProductCode>2262075838680</ProductCode>
            	<ProductDescription>AMERIC</ProductDescription>
    			<Quantity>1</Quantity>
    			<UnitPrice>2.50</UnitPrice>
            </Line>
    		<Line>
    			<LineNumber>3</LineNumber>
            	<ProductCode>2060075838633</ProductCode>
            	<ProductDescription>CARV</ProductDescription>
    			<Quantity>3</Quantity>
    			<UnitPrice>38.50</UnitPrice>
            </Line>
    		...
        </Invoice>
    	<Invoice>
    		<InvoiceNo>FT D0/1963</InvoiceNo>
       		<InvoiceDate>2015-04-01</InvoiceDate>
            <InvoiceType>FT</InvoiceType>
          	<CustomerID>2</CustomerID>
            <Line>
    			<LineNumber>1</LineNumber>
            	<ProductCode>400</ProductCode>
            	<ProductDescription>GUI</ProductDescription>
    			<Quantity>5</Quantity>
    			<UnitPrice>18.00</UnitPrice>
            </Line>
    		<Line>
    			<LineNumber>2</LineNumber>
            	<ProductCode>500</ProductCode>
            	<ProductDescription>DO</ProductDescription>
    			<Quantity>1</Quantity>
    			<UnitPrice>3.30</UnitPrice>
            </Line>
    		...
        </Invoice>
    	....
    </SalesInvoices>	
    ...


    My code C#:

    XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(txt_FileXML.Text);
                
        XmlNodeList ListInvoices = xmlDoc.GetElementsByTagName("Invoice");
    
        foreach (XmlNode invoiceNode in ListInvoices)
        {
    		l_InvoiceNo = (invoiceNode["InvoiceNo"] != null) ? invoiceNode["InvoiceNo"].InnerText : "";
    		
    		XmlNodeList listLines = invoiceNode["line"].ChildNodes;
    		??? 
    	}	
    	...

    I needed to filter all nodes "Line" in the "Invoice", but only filters the first ...

    What where I am going wrong?

    Regards

    sdfa

    quarta-feira, 5 de agosto de 2015 13:39

Todas as Respostas

  • Hi,

    You can use this:

    XmlNodeList listLines = invoiceNode.SelectNodes("/Line");
    


    Fabio Rosa.

    • Sugerido como Resposta Fabio Rosa sexta-feira, 7 de agosto de 2015 00:40
    quinta-feira, 6 de agosto de 2015 11:03
  • Para retornar somente o primeiro XmlNode utilize SelectSingleNode.

    .//MEUNO é uma forma abreviada de self::node()/descendant-or-self::node()/child:MEUNO

    foreach (XmlNode xNode in ListInvoices)
    {
        // sua lógica
        var line = xNode.SelectSingleNode(".//Line");
        // sua lógica
    }


    ROBERTOSLJUNIOR

    • Sugerido como Resposta robertosljunior domingo, 23 de agosto de 2015 02:52
    sábado, 22 de agosto de 2015 16:56