AJUDA PARA LER UM XML

ANGELOATA 12/02/2014 11:42:43
#434630
Preciso ler a informação de um xml de um campo string e gravar no linq to sql.

A estrutura do XML é essa:

<Report ObjectName=[Ô]Lista de pedidos e detalhes para consultas via API[Ô] StoreName=[Ô]testest[Ô] ExecDate=[Ô]12/02/2014 09:29:33[Ô]>
<Record>
<Field Name=[Ô]Núm[Ô] Value=[Ô]15430[Ô]/>
<Field Name=[Ô]Feito em[Ô] Value=[Ô]10/02/2014 13:11:00[Ô]/>
<Field Name=[Ô]Nome[Ô] Value=[Ô]Angelo[Ô]/>
<Field Name=[Ô]Valor embalagem[Ô] Value=[Ô]0.00[Ô]/>
<Field Name=[Ô]Cor[Ô] Value=[Ô][Ô]/>
<Field Name=[Ô]Desconto cupom[Ô] Value=[Ô]0[Ô]/>
<Field Name=[Ô]ChangeFlagAPI[Ô] Value=[Ô]0[Ô]/>
</Record>
</Report>

Como eu Faço?
KERPLUNK 12/02/2014 13:59:40
#434640
Resposta escolhida
é só isso que tem no XML? Coloque-o inteiro aqui
ANGELOATA 12/02/2014 14:13:27
#434645
a estrutura é essa conforme descrito abaixo:

<Report ObjectName=[Ô]Lista de pedidos e detalhes para consultas via API[Ô] StoreName=[Ô]testest[Ô] ExecDate=[Ô]12/02/2014 09:29:33[Ô]>
<Record>
<Field Name=[Ô]Núm[Ô] Value=[Ô]15430[Ô]/>
<Field Name=[Ô]Feito em[Ô] Value=[Ô]10/02/2014 13:11:00[Ô]/>
<Field Name=[Ô]Nome[Ô] Value=[Ô]Angelo[Ô]/>
<Field Name=[Ô]Valor embalagem[Ô] Value=[Ô]0.00[Ô]/>
<Field Name=[Ô]Cor[Ô] Value=[Ô][Ô]/>
<Field Name=[Ô]Desconto cupom[Ô] Value=[Ô]0[Ô]/>
<Field Name=[Ô]ChangeFlagAPI[Ô] Value=[Ô]0[Ô]/>
</Record>
<Record>
<Field Name=[Ô]Núm[Ô] Value=[Ô]15434[Ô]/>
<Field Name=[Ô]Feito em[Ô] Value=[Ô]10/02/2014 13:11:00[Ô]/>
<Field Name=[Ô]Nome[Ô] Value=[Ô]Angelo[Ô]/>
<Field Name=[Ô]Valor embalagem[Ô] Value=[Ô]0.00[Ô]/>
<Field Name=[Ô]Cor[Ô] Value=[Ô][Ô]/>
<Field Name=[Ô]Desconto cupom[Ô] Value=[Ô]0[Ô]/>
<Field Name=[Ô]ChangeFlagAPI[Ô] Value=[Ô]0[Ô]/>
</Record>
<Record>
<Field Name=[Ô]Núm[Ô] Value=[Ô]15435[Ô]/>
<Field Name=[Ô]Feito em[Ô] Value=[Ô]10/02/2014 13:11:00[Ô]/>
<Field Name=[Ô]Nome[Ô] Value=[Ô]Angelo[Ô]/>
<Field Name=[Ô]Valor embalagem[Ô] Value=[Ô]0.00[Ô]/>
<Field Name=[Ô]Cor[Ô] Value=[Ô][Ô]/>
<Field Name=[Ô]Desconto cupom[Ô] Value=[Ô]0[Ô]/>
<Field Name=[Ô]ChangeFlagAPI[Ô] Value=[Ô]0[Ô]/>
</Record>
</Report>
ANGELOATA 12/02/2014 14:41:06
#434649
inclusive fiz ate essa parte:

p_xml = [Ô]conforme exemplo enviado[Ô]

Dim document As XDocument = XDocument.Parse(p_xml)

Dim query = From p In document.Descendants([Ô]Record[Ô]) _
Select p

For Each record In query

MessageBox.Show(record.Element([Ô]Field[Ô]).Attribute([Ô]Name[Ô]).Value + vbCrLf +
record.Element([Ô]Field[Ô]).Attribute([Ô]Value[Ô]).Value)

** Estou com dificuldade aqui **


Next
ROBIU 12/02/2014 14:43:44
#434650
Veja neste link: VB .NET - Lendo arquivos XML. No final do Tutorial tem outros artigos sobre o assunto
KERPLUNK 12/02/2014 14:47:31
#434651
Certo, este é o arquivo completo, não tem mais nada? Porque até consegui gerar a classe dele, mas acho que está faltando algo.
ANGELOATA 12/02/2014 14:50:03
#434652
é so isso mesmo... nao coloquei inteiro dentro do campos record pois so ia ter linguiça desnecessaria
KERPLUNK 12/02/2014 15:19:34
#434653
Ok, primeiramente, vamos criar as classes necessárias. Crie uma nova classe qualquer e substitua o conteúdo por isso:

[ô]------------------------------------------------------------------------------
[ô] <auto-generated>
[ô] This code was generated by a tool.
[ô] Runtime Version:4.0.30319.17929
[ô]
[ô] Changes to this file may cause incorrect behavior and will be lost if
[ô] the code is regenerated.
[ô] </auto-generated>
[ô]------------------------------------------------------------------------------

Imports System.Xml.Serialization

[ô]
[ô] This source code was auto-generated by xsd, Version=4.0.30319.1.
[ô]


[ô][ô][ô] <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute([Ô]xsd[Ô], [Ô]4.0.30319.1[Ô])> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute([Ô]code[Ô])> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType := True)> _
<System.Xml.Serialization.XmlRootAttribute([Namespace] := [Ô][Ô], IsNullable := False)> _
Public Partial Class Report

Private recordField As ReportRecord()

Private objectNameField As String

Private storeNameField As String

Private execDateField As String

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute([Ô]Record[Ô], Form := System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Record() As ReportRecord()
Get
Return Me.recordField
End Get
Set
Me.recordField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlAttributeAttribute> _
Public Property ObjectName() As String
Get
Return Me.objectNameField
End Get
Set
Me.objectNameField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlAttributeAttribute> _
Public Property StoreName() As String
Get
Return Me.storeNameField
End Get
Set
Me.storeNameField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlAttributeAttribute> _
Public Property ExecDate() As String
Get
Return Me.execDateField
End Get
Set
Me.execDateField = value
End Set
End Property
End Class

[ô][ô][ô] <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute([Ô]xsd[Ô], [Ô]4.0.30319.1[Ô])> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute([Ô]code[Ô])> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType := True)> _
Public Partial Class ReportRecord

Private fieldField As ReportRecordField()


[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute([Ô]Field[Ô], Form := System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Field() As ReportRecordField()
Get
Return Me.fieldField
End Get
Set
Me.fieldField = value
End Set
End Property


End Class

[ô][ô][ô] <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute([Ô]xsd[Ô], [Ô]4.0.30319.1[Ô])> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute([Ô]code[Ô])> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType := True)> _
Public Partial Class ReportRecordField

Private nameField As String

Private valueField As String

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlAttributeAttribute> _
Public Property Name() As String
Get
Return Me.nameField
End Get
Set
Me.nameField = value
End Set
End Property

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlAttributeAttribute> _
Public Property Value() As String
Get
Return Me.valueField
End Get
Set
Me.valueField = value
End Set
End Property
End Class

[ô][ô][ô] <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute([Ô]xsd[Ô], [Ô]4.0.30319.1[Ô])> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute([Ô]code[Ô])> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType := True)> _
<System.Xml.Serialization.XmlRootAttribute([Namespace] := [Ô][Ô], IsNullable := False)> _
Public Partial Class NewDataSet

Private itemsField As Report()

[ô][ô][ô] <remarks/>
<System.Xml.Serialization.XmlElementAttribute([Ô]Report[Ô])> _
Public Property Items() As Report()
Get
Return Me.itemsField
End Get
Set
Me.itemsField = value
End Set
End Property
End Class



Depois, para ler o xml, faça um desserializador simples:

Dim ser As New XmlSerializer(GetType(Report))
Dim resultadoFinal As Report
Dim path As String = [Ô]C:\program files\microsoft visual studio 10.0\vc\arq.xml[Ô]
Using reader As XmlReader = XmlReader.Create(path)
resultadoFinal = DirectCast(ser.Deserialize(reader), Report)
End Using



Neste ponto, a variável [Ô]resultadoFinal[Ô], vai conter o conteúdo do xml e pronto.
ANGELOATA 12/02/2014 16:08:01
#434656
Essa classe vc criou onde no VS2010?
KERPLUNK 12/02/2014 16:15:09
#434657
Na verdade criei com o xsd, de modo automático baseado no xml. Porque? Está tendo algum problema?
ANGELOATA 12/02/2014 16:22:46
#434659
Quero entender como funciona, onde é feito, pois tenho uma serie de xml diferentes para ler e gostaria de fazer o mesmo processo que vc exemplificou.
Página 1 de 3 [26 registro(s)]
Tópico encerrado , respostas não são mais permitidas