ERROR - Catch Xelement C#

Asked

Viewed 379 times

2

Hello. I’m trying to get a Node of an xml Nfe can this appearing image error. inserir a descrição da imagem aqui

At the beginning of the class a variable was created private XDocument xDoc; The xml file is inside the debug folder.

Even putting so appears the same Error:

XElement _xDetModelo = new XElement(xDoc.Element("nfeProc").Element("NFe").Element("infNFe").Element("det"));

Below part of the XML code I’m trying to get:

<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
    <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
        <infNFe Id="NFeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" versao="3.10">
            <ide>
                    ...
            </ide>
            <emit>
                    ...
            </emit>
            <dest>
                    ...
            </dest>
            <det nItem="1">
                <prod>
                    ...

2 answers

3

Although you have already solved with the answer of colleague Virgilio Novic I will show another way to do this, you can create the classes referring to Nfe.

It may be useful for you or someone else reading.

There is an app called xsd.exe. Roughly sums it up, he takes the schema and returns you a class.

You must download Nfe schemas and save them in some folder.

Do the following as in the image below: inserir a descrição da imagem aqui

Logically override the path according to where you saved the Nfe schema.

Inside the folder of xsd.exe it will create a file .cs as in the image below:

inserir a descrição da imagem aqui

After this you add the class in your project, you can use Nfe as if it were an object.

inserir a descrição da imagem aqui

Complementing:

You can put this code block inside a try\catch, so if xml is not valid, an exception will be generated. As you generated the class from the schema, only the xml’s that are valid will be deserialized, this will save you a lot of headache.

1


The file has namespace, and at the time of the search for elements he needs to be informed:

XNamespace nameSpace = "http://www.portalfiscal.inf.br/nfe";
string xml = System.IO.File.ReadAllText("doc.xml");

XDocument xDoc = XDocument.Parse(xml);
XElement element = xDoc.Element(nameSpace + "nfeProc")
          .Element(nameSpace + "NFe")
          .Element(nameSpace + "infNFe")
          .Element(nameSpace + "det");

Example in Soen, demonstrates this.

Edited:

Func<string, XName> item = delegate(string value)
{
    XNamespace nameSpace = "http://www.portalfiscal.inf.br/nfe";
    return nameSpace + value;
};


string xml = System.IO.File.ReadAllText("doc.xml");

XDocument xDoc = XDocument.Parse(xml);           

var element = from el in xDoc.Element(item("nfeProc"))
        .Element(item("NFe"))
        .Element(item("infNFe"))
        .Element(item("det"));
  • Virgilio Novic Thank you, your solution worked. Just a doubt. is it possible to leave the namespace "Universal" to my variable xDoc? so I wouldn’t have to keep putting namespace + "...".

  • @Luismedeiros, nothing ... I had also searched before this, maybe it happens because it can have several namespace at each node! What can be done a class with functions static to fix it. but, I don’t know if that would be good...

  • @Luismedeiros I made an edition and put an anonymous function to assist ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.