4
Well, I’m trying to do the reading for import of XMLs
of the Electronic Tax Notes and I’m having a lot of difficulty in disemboweling the ICMS
of the blessed.
No problems to reach the object ICMS
:
object objeto = ListaItens[i].imposto.Items[0]
This returns an object of the type TNFeInfNFeDetImpostoICMS
and here is the description of this class:
public partial class TNFeInfNFeDetImpostoICMS {
private object itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ICMS00", typeof(TNFeInfNFeDetImpostoICMSICMS00))]
[System.Xml.Serialization.XmlElementAttribute("ICMS10", typeof(TNFeInfNFeDetImpostoICMSICMS10))]
[System.Xml.Serialization.XmlElementAttribute("ICMS20", typeof(TNFeInfNFeDetImpostoICMSICMS20))]
[System.Xml.Serialization.XmlElementAttribute("ICMS30", typeof(TNFeInfNFeDetImpostoICMSICMS30))]
[System.Xml.Serialization.XmlElementAttribute("ICMS40", typeof(TNFeInfNFeDetImpostoICMSICMS40))]
[System.Xml.Serialization.XmlElementAttribute("ICMS51", typeof(TNFeInfNFeDetImpostoICMSICMS51))]
[System.Xml.Serialization.XmlElementAttribute("ICMS60", typeof(TNFeInfNFeDetImpostoICMSICMS60))]
[System.Xml.Serialization.XmlElementAttribute("ICMS70", typeof(TNFeInfNFeDetImpostoICMSICMS70))]
[System.Xml.Serialization.XmlElementAttribute("ICMS90", typeof(TNFeInfNFeDetImpostoICMSICMS90))]
[System.Xml.Serialization.XmlElementAttribute("ICMSPart", typeof(TNFeInfNFeDetImpostoICMSICMSPart))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN101", typeof(TNFeInfNFeDetImpostoICMSICMSSN101))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN102", typeof(TNFeInfNFeDetImpostoICMSICMSSN102))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN201", typeof(TNFeInfNFeDetImpostoICMSICMSSN201))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN202", typeof(TNFeInfNFeDetImpostoICMSICMSSN202))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN500", typeof(TNFeInfNFeDetImpostoICMSICMSSN500))]
[System.Xml.Serialization.XmlElementAttribute("ICMSSN900", typeof(TNFeInfNFeDetImpostoICMSICMSSN900))]
[System.Xml.Serialization.XmlElementAttribute("ICMSST", typeof(TNFeInfNFeDetImpostoICMSICMSST))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
What I would like to do is to sweep up all the "types" of ICMS
to take their properties, but I couldn’t find any way to make a loop for
disembobulating each element.
Can help?
EDIT:
One thing I realized in practice (I don’t have much tax knowledge) is that each item in the XML
will present only one of these classes of ICMS
encapsulated, so all you had to do was find a way to unbox
of the object choosing at runtime the class contained. Is there any way to do this?
EDIT2:
I do the deserialization as follows:
I have a class Serializer
:
public static class Serializer
{
public static T Deserialize<T>(this XElement xElement)
{
using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xElement.ToString())))
{
var xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(memoryStream);
}
}
public static XElement Serialize<T>(this object o)
{
using (var memoryStream = new MemoryStream())
{
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(streamWriter, o);
return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
}
}
}
}
And a class ProcNFe_v3_10
generated from the .xsd
I picked up on the farm site (I will not post because it is too extensive).
So first I carry one XElement
with my .xml
:
XElement xElement = XElement.Load(ofdXml.FileName);
Being ofdXml
one OpenFileDialog
.
And later I call the method deserializer
of my class passing TNfeProc
as a type T
(which is the corresponding type of my grade, contained within the class ProcNFe_v3_10
) and the xElement
loaded as attribute, as follows:
TNfeProc NFCarregada = Serializer.Deserialize<TNfeProc>(xElement);
And voilà, I have mine XML
deserialized.
http://answall.com/questions/44130/leitura-xml-nfe?rq=1
– rubStackOverflow
Actually @Hstackoverflow, I’m already utilizing the deserialization of the
XML
from the original Nfe schema. My problem is getting to that specific information. The rest of the data I’ve been able to extract.– Cassio Milanelo
Are you doing deserialization? You can post the code?
– rubStackOverflow
Sorry for the delay. I edited the question adding the deserialization.
– Cassio Milanelo
Can be with LINQ?
– PauloHDSousa
It will be a great time for me to learn how to work with LINQ. Send!
– Cassio Milanelo