0

I have these XML file:

<cteProc xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <CTe xmlns="http://www.portalfiscal.inf.br/cte">
        <infCte versao="1.04" Id="CTexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
            <ide>
                <compl>
                    <emit>
                        <rem>
                            <CNPJ>11111111111</CNPJ>
                            <IE>2222222</IE>
                            <xNome>Teste</xNome>
                            <enderReme>
                                <xLgr>xxxxxxx xxxxxx</xLgr>
                                <nro>S/N</nro>
                                <xCpl>AREA C</xCpl>
                                <xBairro>PARQ. xxxxxx</xBairro>
                                <cMun>125455</cMun>
                                <xMun>xxxxxx</xMun>
                                <CEP>548848</CEP>
                                <UF>AA</UF>
                                <xPais>BRASIL</xPais>
                            </enderReme>
                            <infNFe>
                                **<chave>1</chave>**
                                **<chave>2</chave>**
                                **<chave>3</chave>**
                            </infNFe>
                        </rem>
                        <exped>
                            <CNPJ>2342342342342</CNPJ>
                            <IE>15342683242345480</IE>
                                ...........................

And I need to get values and put inside a string

I try to do this:

var ListaChave = new List<string>();

var lista = (from c in xDoc.Descendants(ns + "/rem/chave") select c.Value).ToList();

foreach (string s in lista)
  {
    add the values.....
  }

But the s var is null. I don´t know how to get these values. Anybody can help me please!?

2
  • What value has ns variable when you call Descendants() method? Commented Jun 20, 2013 at 16:44
  • 1
    Change your Descendants portion to Descendants(ns + "/rem/infNFe/chave"). The chave element is a child of theinfNFe element. Commented Jun 20, 2013 at 16:44

3 Answers 3

3

Use linq to xml

XDocument doc = XDocument.Load("XMLFile1.xml");

XNamespace ns = @"http://www.portalfiscal.inf.br/cte";

List<string> strList = doc.Descendants(ns+"rem").Descendants(ns+"chave").Select(e => e.Value).ToList();

and alternatively, you can have more control by doing things like

Sign up to request clarification or add additional context in comments.

Comments

1

You're missing an element in the path you passed to Descendants. In your XML document the chave elements are children of infNFe. Your LINQ query is looking for chave elements under "rem", and not finding any, hence the null result.

Change your query to this:

var lista = (from c in xDoc.Descendants(ns + "/rem/infNFe/chave") 
             select c.Value).ToList();

And you should get what you're looking for, as long as the ns is set correctly.

Comments

0

You can try with this code by just adding the infNFe:

var ListaChave = new List<string>();

var lista = (from c in xDoc.Descendants(ns + "/rem/infNFe/chave") 
             select c.Value).ToList();

foreach (string s in lista)
{
   add the values.....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.