简体   繁体   中英

C# Load XML Child Node to textbox

I need to load some specific child nodes to textboxes but they are structured as child nodes inside a parent node.

The XML structure:

<nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
    <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
            <det nItem="1">
                <prod>
                    <cProd>09</cProd>
                    <cEAN />
                    <xProd>CHAPEU DE PALHA SOBRAL</xProd>
                </prod>
                <imposto>
                    <vTotTrib>30.23</vTotTrib>
                    <ICMS>
                        <ICMSSN101>
                            <orig>0</orig>
                            <CSOSN>101</CSOSN>
                            <pCredSN>2.5600</pCredSN>
                            <vCredICMSSN>2.46</vCredICMSSN>
                        </ICMSSN101>
                    </ICMS>
                </imposto>
            </det>
    </protNFe>
</nfeProc>

Inside the XMLs there are various nodes. For each of then, I need to load some data. My code so far is:

                XmlNodeList xndet = xml.GetElementsByTagName("det");
                foreach (XmlNode xn in xndet)

                {
                    string cProd =  xn.SelectSingleNode("/prod/cProd").InnerText.ToString();
                    txCPROD.Text = cProd;

                    string vCredICMSSN = xn.SelectSingleNode("/imposto/ICMS/ICMSSN101/vCredICMSSN").InnerText.ToString();
                    txICMSSN.Text = vCredICMSSN;

                    // do other things...

                } 

The error occurs here:

"string cProd =  xn.SelectSingleNode("/prod/cProd").InnerText.ToString();

My logic says that inside the xn node the string will receive the inner text of the cProd element.

But I get an unhandled exception of type 'System.NullReferenceException' occurred

Your XML is invalid, the closing tag for <NFe> is </protNFe> so I presume you have made a copy/paste error otherwise your code would not run at all.

I prefer System.Xml.Linq.XDocument over the System.Xml classes, I find it easier to work with.

Here is a solution to get a list of values that are in the cProd elements. Here I am ignoring the namespace by using the element's Name.LocalName property. Only do this if you are sure you don't need to bother with the namespace. Based on your sample xml I don't think you need to since there are no other elements by the same name but in a different namespace that I can see.

string theXML = @"<nfeProc versao=""3.10"" xmlns=""http://www.portalfiscal.inf.br/nfe""><NFe xmlns=""http://www.portalfiscal.inf.br/nfe""><det nItem=""1""><prod><cProd>09</cProd><cEAN /><xProd>CHAPEU DE PALHA SOBRAL</xProd></prod><imposto><vTotTrib>30.23</vTotTrib><ICMS><ICMSSN101><orig>0</orig><CSOSN>101</CSOSN><pCredSN>2.5600</pCredSN><vCredICMSSN>2.46</vCredICMSSN></ICMSSN101></ICMS></imposto></det><det nItem=""2""><prod><cProd>10</cProd><cEAN /><xProd>CHAPEU DE PALHA SOBRAL</xProd></prod><imposto><vTotTrib>30.23</vTotTrib><ICMS><ICMSSN101><orig>0</orig><CSOSN>101</CSOSN><pCredSN>2.5600</pCredSN><vCredICMSSN>2.46</vCredICMSSN></ICMSSN101></ICMS></imposto></det></NFe></nfeProc>";
XDocument doc = XDocument.Parse(theXML);
var cProdNodes = doc.Descendants().Elements().Where (d => d.Name.LocalName == "cProd");
List<string> cProdValues = cProdNodes.Select (pn => pn.Value).ToList();

If you want to play it safe with the namespace, you can do this:

XDocument doc = XDocument.Parse(theXML);
XNamespace ns = "http://www.portalfiscal.inf.br/nfe";
var cProdNodes = doc.Descendants().Elements().Where (d => d.Name == ns + "cProd");
List<string> cProdValues = cProdNodes.Select (pn => pn.Value).ToList();

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