LEITURA DE XML DE NFSE

JCM0867 07/12/2017 23:58:08
#478346
Olá Pessoal
Estou tendo dificuldade de Pegar informações de um XML de NFS-E
Tomei como exemplo pegar o Numero da Nota Fiscal, tará outras informações que irei pegar tb
Usei:

Imports System.Xml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
[ô]Create the XML Document

m_xmld = New XmlDocument()
[ô]Load the Xml file

m_xmld.Load([Ô]C:\XML
fse_201700000000001.xml[Ô])
[ô]Get the list of name nodes

[txt-color=#e80000]m_nodelist = m_xmld.SelectNodes([Ô]/CompNfse/Nfse/InfNfse[Ô])[/txt-color]
[ô]Loop through the nodes

For Each m_node In m_nodelist

Dim NumeroNota = m_node.Attributes.GetNamedItem([Ô]Numero[Ô]).Value

MsgBox([Ô]Numero: [Ô] & NumeroNota)
Next
Catch errorVariable As Exception
MsgBox(errorVariable.ToString())
End Try

End Sub
End Class

Quero pegar o Numero da Nota e acredito que o erro está na linha em vermelho, qual o erro? ele não executa o Loop
segue abaixo começo do arquivo XML

[txt-color=#0000f0]<?xml version=[Ô]1.0[Ô] encoding=[Ô]UTF-8[Ô]?>

-<CompNfse xmlns=[Ô]http://www.abrasf.org.br/nfse.xsd[Ô]>


-<Nfse xmlns=[Ô]http://www.abrasf.org.br/nfse.xsd[Ô] versao=[Ô]1.00[Ô]>


-<InfNfse Id=[Ô]nfse[Ô]>

<Numero>201700000000001</Numero>

<CodigoVerificacao>88785861</CodigoVerificacao>

<DataEmissao>2017-12-07T18:30:09</DataEmissao>

...[/txt-color]

Grato

KERPLUNK 08/12/2017 10:01:05
#478347
Resposta escolhida
Use desserialização...
JCM0867 08/12/2017 10:44:41
#478349
Bom, não sei o que desserialização, vou pesquisar
JCM0867 08/12/2017 12:08:26
#478352
Citação:

:
Use desserialização...



Não quer funcionar o Exemplo, os valores vem todos zerados

[txt-color=#0000f0]Imports System
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal

[ô] A custom method used to calculate price per item.
[ô]Public Sub Calculate()
[ô] LineTotal = UnitPrice * Quantity
[ô]End Sub
End Class

Public Shared Sub Main()
Dim t As New Test()
[ô] Read a purchase order.
t.DeserializeObject([Ô]C:\XML\simple.xml[Ô])
End Sub

Private Sub DeserializeObject(ByVal filename As String)
Console.WriteLine([Ô]Reading with XmlReader[Ô])

[ô] Create an instance of the XmlSerializer specifying type and namespace.
Dim serializer As New XmlSerializer(GetType(OrderedItem))

[ô] A FileStream is needed to read the XML document.
Dim fs As New FileStream(filename, FileMode.Open)
Dim reader As XmlReader = XmlReader.Create(fs)

[ô] Declare an object variable of the type to be deserialized.
Dim i As OrderedItem

[ô] Use the Deserialize method to restore the object[ô]s state.
i = CType(serializer.Deserialize(reader), OrderedItem)
fs.Close()

[ô] Write out the properties of the object.
MsgBox(i.ItemName & ControlChars.Tab &
i.Description & ControlChars.Tab &
i.UnitPrice & ControlChars.Tab &
i.Quantity & ControlChars.Tab &
i.LineTotal)
End Sub
[/txt-color]

XML:
<?xml version=[Ô]1.0[Ô]?>
-<OrderedItem xmlns:money=[Ô]http://www.cohowinery.com[Ô] xmlns:inventory=[Ô]http://www.cpandl.com[Ô]>
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem
KERPLUNK 08/12/2017 13:25:27
#478353
Você gerou a classe base como? Acho que está faltando coisa ae
KERPLUNK 08/12/2017 13:52:16
#478354
Além disso, não vejo porque usar o XmlReader:

Dim path As String = [Ô]C: \SuaPasta\Arquivo.xml[Ô]
Dim serializer As XmlSerializer = New XmlSerializer(GetType(OrderedItem))
Dim reader As StreamReader = New StreamReader(path)
Dim item As OrderedItem = CType(serializer.Deserialize(reader),OrderedItem)
reader.Close

E a classe, pelo XML que você postou fica:

<System.SerializableAttribute(), _
System.ComponentModel.DesignerCategoryAttribute([Ô]code[Ô]), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
System.Xml.Serialization.XmlRootAttribute(Namespace:=[Ô][Ô], IsNullable:=false)> _
Public Class OrderedItem

Private itemNameField As String

Private descriptionField As String

Private unitPriceField As Decimal

Private quantityField As Byte

Private lineTotalField As Byte

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute(Namespace:=[Ô]http://www.cpandl.com[Ô])> _
Public Property ItemName As String
Get
Return Me.itemNameField
End Get
Set
Me.itemNameField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute(Namespace:=[Ô]http://www.cpandl.com[Ô])> _
Public Property Description As String
Get
Return Me.descriptionField
End Get
Set
Me.descriptionField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute(Namespace:=[Ô]http://www.cohowinery.com[Ô])> _
Public Property UnitPrice As Decimal
Get
Return Me.unitPriceField
End Get
Set
Me.unitPriceField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute(Namespace:=[Ô]http://www.cpandl.com[Ô])> _
Public Property Quantity As Byte
Get
Return Me.quantityField
End Get
Set
Me.quantityField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute(Namespace:=[Ô]http://www.cohowinery.com[Ô])> _
Public Property LineTotal As Byte
Get
Return Me.lineTotalField
End Get
Set
Me.lineTotalField = value
End Set
End Property
End Class

KERPLUNK 08/12/2017 16:43:58
#478358
Mais uma vez, a maldição do tópico morto... basta eu postar algum código e pronto, sem resposta às vezes por meses.
JCM0867 09/12/2017 10:55:09
#478362
Nada disso, eu sempre respondi e encerrei.
E foi ontem!
Estava entrando para dizer que deu certo com teu Código que passou. Só estava testando e adaptando a minha situação.
Mas valew,

Grato

Tópico encerrado , respostas não são mais permitidas