• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

xml serilization

Hi,
Advance thanks.
I have vb class and i am trying to serialize it. I am getting multiple tags and i don't know what mistake i am doing here. Please help me and i don't like major change here since i knew i am missing something and i want somebody to point it out. I was fed up in seeing this code again and again. Please help somebody...

Function in xaml page:
=================
 Private Sub CreateSampleXML(ByVal PerChildAList As List(Of String()), ByVal PerCsawAList As List(Of String()))
        Dim objArray(PerChildAList.Count - 1) As report
        Dim objReport As New report()
        Dim objSusArray(PerCsawAList.Count - 1) As suspect
        Dim xs As XmlSerializer = New XmlSerializer(GetType(report()))
        Dim ms As MemoryStream = New MemoryStream
        Dim bt As Byte() = ms.ToArray
        Dim objInd As Integer = 0
        Dim objVictim(PerChildAList.Count - 1) As victim
        Dim objSuspect(PerCsawAList.Count - 1) As suspect

        For Each l In PerChildAList
            objVictim(objInd) = New victim()
            Dim VicPerson As New objVictimPerson()
            Dim VictimAddress As New objaddress()
            Dim VictimIP As New objIpCaptureEvent()
            VicPerson.firstName = l(5)
            VicPerson.lastName = l(6)
            VicPerson.email = "test@test.com"
            VictimAddress.address = "Street Address information is missing in Child page"
            VictimAddress.city = l(14)
            VictimAddress.zipCode = l(9)
            VictimAddress.state = l(28)
            VictimAddress.nonUsaState = l(39)
            VictimAddress.country = l(15)
            VicPerson.age = l(1)
            VicPerson.dateOfBirth = l(11)
            VictimIP.ipAddress = ipname
            VictimIP.eventName = "LOGIN"
            VictimIP.cookieInfo = "NO cookie information found"
            VictimIP.dateTime = Format(CDate(DateTime.Now), "yyyy-MM-dd HH:mm:ss") 'l(17)
            VictimIP.timeZone = timezone.StandardName
            objVictim(objInd).victimPerson = VicPerson
            objVictim(objInd).victimPerson.address = VictimAddress
            objVictim(objInd).ipCaptureEvent = VictimIP
            objReport.victim = objVictim
            objInd += 1
            If objInd = PerChildAList.Count Then
                objInd = 0
                For Each cl In PerCsawAList
                    objSuspect(objInd) = New suspect()
                    Dim SuspectPerson As New objSuspectPerson()
                    Dim SuspectAddress As New objCsawAddress()
                    Dim SuspectIP As New objCsawIpCaptureEvent()
                    SuspectPerson.firstName = cl(5)
                    SuspectPerson.lastName = cl(6)
                    SuspectPerson.email = "test@test.com"
                    SuspectAddress.address = "Street Address information is missing in Child page"
                    SuspectAddress.city = cl(14)
                    SuspectAddress.zipCode = cl(9)
                    SuspectAddress.state = cl(28)
                    SuspectAddress.nonUsaState = cl(39)
                    SuspectAddress.country = cl(15)
                    SuspectPerson.age = cl(1)
                    SuspectPerson.dateOfBirth = cl(11)
                    SuspectIP.ipAddress = ipname
                    SuspectIP.eventName = "LOGIN"
                    SuspectIP.cookieInfo = "NO cookie information found"
                    SuspectIP.dateTime = Format(CDate(DateTime.Now), "yyyy-MM-dd HH:mm:ss") 'l(17)
                    SuspectIP.timeZone = timezone.StandardName
                    objSuspect(objInd).suspectPerson = SuspectPerson
                    objSuspect(objInd).suspectPerson.address = SuspectAddress
                    objSuspect(objInd).ipCaptureEvent = SuspectIP
                    objReport.suspect = objSuspect
                    objInd += 1
                Next
            End If
        Next
        xs = New XmlSerializer(objReport.GetType)
        xs.Serialize(ms, objReport)
        bt = ms.ToArray
        ms.Close()
        MessageBox.Show(System.Text.Encoding.UTF8.GetString(bt, 0, bt.Length))
    End Sub


VB class file
==========
Imports System
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports Microsoft.VisualBasic

Public Class report
    Public victim() As victim
    Public suspect() As suspect

    'Public victim As victim
    'Public suspect As suspect
End Class

Public Class victim
    Public victimPerson As objVictimPerson
    Public ipCaptureEvent As objIpCaptureEvent

    'Public victimPerson() As objVictimPerson
    'Public ipCaptureEvent() As objIpCaptureEvent

End Class
Public Class objVictimPerson

    Public firstName As String
    Public lastName As String
    Public phone As String
    Public email As String
    Public address As objaddress
    Private _age As String

    Public Property age As String
        Get
            Return _age
        End Get
        Set(ByVal value As String)
            _age = value
        End Set
    End Property

    Private _dateOfBirth As String

    Public Property dateOfBirth() As String
        Get
            Return _dateOfBirth
        End Get
        Set(ByVal value As String)
            _dateOfBirth = value
        End Set
    End Property

End Class
Public Class objIpCaptureEvent
    Private _ipAddress As String

    Public Property ipAddress() As String
        Get
            Return _ipAddress
        End Get
        Set(ByVal value As String)
            _ipAddress = value
        End Set
    End Property

    Private _eventName As String

    Public Property eventName() As String
        Get
            Return _eventName
        End Get
        Set(ByVal value As String)
            _eventName = value
        End Set
    End Property

    Private _cookieInfo As String

    Public Property cookieInfo() As String
        Get
            Return _cookieInfo
        End Get
        Set(ByVal value As String)
            _cookieInfo = value
        End Set
    End Property

    Private _dateTime As String

    Public Property dateTime() As String
        Get
            Return _dateTime
        End Get
        Set(ByVal value As String)
            _dateTime = value
        End Set
    End Property

    Private _timeZone As String

    Public Property timeZone() As String
        Get
            Return _timeZone
        End Get
        Set(ByVal value As String)
            _timeZone = value
        End Set
    End Property
End Class

Public Class objaddress
    Private _address As String

    Public Property address() As String
        Get
            Return _address
        End Get
        Set(ByVal value As String)
            _address = value
        End Set
    End Property
    Private _city As String

    Public Property city() As String
        Get
            Return _city
        End Get
        Set(ByVal value As String)
            _city = value
        End Set
    End Property
    Private _zipCode As String

    Public Property zipCode() As String
        Get
            Return _zipCode
        End Get
        Set(ByVal value As String)
            _zipCode = value
        End Set
    End Property
    Private _state As String

    Public Property state() As String
        Get
            Return _state
        End Get
        Set(ByVal value As String)
            _state = value
        End Set
    End Property
    Private _nonUsaState As String

    Public Property nonUsaState() As String
        Get
            Return _nonUsaState
        End Get
        Set(ByVal value As String)
            _nonUsaState = value
        End Set
    End Property
    Private _country As String

    Public Property country() As String
        Get
            Return _country
        End Get
        Set(ByVal value As String)
            _country = value
        End Set
    End Property
End Class
Public Class suspect
    Public suspectPerson As objSuspectPerson
    Public ipCaptureEvent As objCsawIpCaptureEvent

End Class
Public Class objSuspectPerson
    Public firstName As String
    Public lastName As String
    Public phone As String
    Public email As String
    Public address As objCsawAddress
    Private _age As String

    Public Property age As String
        Get
            Return _age
        End Get
        Set(ByVal value As String)
            _age = value
        End Set
    End Property

    Private _dateOfBirth As String

    Public Property dateOfBirth() As String
        Get
            Return _dateOfBirth
        End Get
        Set(ByVal value As String)
            _dateOfBirth = value
        End Set
    End Property

End Class
Public Class objCsawIpCaptureEvent
    Private _ipAddress As String

    Public Property ipAddress() As String
        Get
            Return _ipAddress
        End Get
        Set(ByVal value As String)
            _ipAddress = value
        End Set
    End Property

    Private _eventName As String

    Public Property eventName() As String
        Get
            Return _eventName
        End Get
        Set(ByVal value As String)
            _eventName = value
        End Set
    End Property

    Private _cookieInfo As String

    Public Property cookieInfo() As String
        Get
            Return _cookieInfo
        End Get
        Set(ByVal value As String)
            _cookieInfo = value
        End Set
    End Property

    Private _dateTime As String

    Public Property dateTime() As String
        Get
            Return _dateTime
        End Get
        Set(ByVal value As String)
            _dateTime = value
        End Set
    End Property

    Private _timeZone As String

    Public Property timeZone() As String
        Get
            Return _timeZone
        End Get
        Set(ByVal value As String)
            _timeZone = value
        End Set
    End Property
End Class

Public Class objCsawAddress
    Private _address As String

    Public Property address() As String
        Get
            Return _address
        End Get
        Set(ByVal value As String)
            _address = value
        End Set
    End Property
    Private _city As String

    Public Property city() As String
        Get
            Return _city
        End Get
        Set(ByVal value As String)
            _city = value
        End Set
    End Property
    Private _zipCode As String

    Public Property zipCode() As String
        Get
            Return _zipCode
        End Get
        Set(ByVal value As String)
            _zipCode = value
        End Set
    End Property
    Private _state As String

    Public Property state() As String
        Get
            Return _state
        End Get
        Set(ByVal value As String)
            _state = value
        End Set
    End Property
    Private _nonUsaState As String

    Public Property nonUsaState() As String
        Get
            Return _nonUsaState
        End Get
        Set(ByVal value As String)
            _nonUsaState = value
        End Set
    End Property
    Private _country As String

    Public Property country() As String
        Get
            Return _country
        End Get
        Set(ByVal value As String)
            _country = value
        End Set
    End Property
End Class


There should be only one VICTIM tag and SUSPECT  tag..

Kind regards,
Pooja
ScreenHunter-10-Oct.-18-14.15.gif
0
poojasureshkumar
Asked:
poojasureshkumar
  • 10
  • 6
1 Solution
 
MikeTooleCommented:
By the look of it the only purpose of the Report class is to produce XML output, is that true?
If so it would be much easier to achieve using VB's XML Literals support.
I presume that if there is only one <victim> tag it should contain multiple <victimPerson> tags, and that the same applies to <suspect>?

0
 
poojasureshkumarAuthor Commented:
Hi Mike,
Yes the report class has to produce xml output.
Format is like this,
<report>
<victim>
                                <victimPerson>
                                                <firstName>Karen</firstName>
                                                <lastName>Spesard</lastName>
                                                <phone type="Mobile">555-000-3696</phone>
                                                <email>kSpesard@email.com</email>
                                                <address type="Business">
                                                                <address>200 King St</address>
                                                                <city>Victoria</city>
                                                                <zipCode>001122</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>20</age>
                                                <dateOfBirth>1901-01-01</dateOfBirth>
                                </victimPerson>
                                <screenName>kspesard</screenName>
                                <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>192.168.0.1</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </victim>
                <victim>
                                <victimPerson>
                                                <firstName>Karen</firstName>
                                                <lastName>Spesard</lastName>
                                                <phone type="Mobile">555-000-3696</phone>
                                                <email>kSpesard@email.com</email>
                                                <address type="Business">
                                                                <address>200 King St</address>
                                                                <city>Victoria</city>
                                                                <zipCode>001122</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>20</age>
                                                <dateOfBirth>1901-01-01</dateOfBirth>
                                </victimPerson>
                                <screenName>kspesard</screenName>
                                <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>192.168.0.1</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </victim>

similarly for suspect

</report>


Kind regards,
PS
0
 
MikeTooleCommented:
As an example to illustrate my previous comment, the attached code produces the output listed below.
It's intended only as an example of the technique so that, for instance, it only shows the construction of the XML hierarchy rather than creating a final XML document

 
<root>    
<victim>      
<victimPerson>        
 <firstName>Tom</firstName>        
 <lastName>Jones</lastName>        
 <eMail>test@test.com</eMail>        
 <address>          
   <street />          
   <city />          
   <zip />          
  <etc />        
 </address>      
</victimPerson>      
<victimPerson>      
  <firstName>Dick</firstName>      
  <lastName>Turpin</lastName>      
  <eMail>test@test.com</eMail>    
    <address>      
     <street />      
     <city />    
      <zip />    
      <etc />    
   </address>    
 </victimPerson>      
<victimPerson>      
  <firstName>Harry</firstName>    
  <lastName>Lime</lastName>      
  <eMail>test@test.com</eMail>    
  <address>    
      <street />    
      <city />    
      <zip />    
     <etc />      
  </address>    
  </victimPerson>    
</victim>    
<suspect />  
</root>"

Public Shared Function CreateDoc() As XElement
        Dim PerChildAList As New List(Of String()) From {New String() {"", "", "", "", "", "Tom", "Jones"},
                                                         New String() {"", "", "", "", "", "Dick", "Turpin"},
                                                         New String() {"", "", "", "", "", "Harry", "Lime"}}
        Dim report = <root>
                         <victim/>
                         <suspect/>
                     </root>
        PerChildAList.ForEach(Sub(l) report.Element("victim").Add(GetVictimPerson(l)))
        Return report
    End Function
    Public Shared Function GetVictimPerson(L As String())
        Dim v = <victimPerson>
                    <firstName><%= L(5) %></firstName>
                    <lastName><%= L(6) %></lastName>
                    <eMail>test@test.com</eMail>
                    <address>
                        <street/>
                        <city/>
                        <zip/>
                        <etc/>
                    </address>
                </victimPerson>
        Return v
    End Function

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
poojasureshkumarAuthor Commented:
Hi Mike,
Thanks for the response. I followed your logic and i am still getting it as earlier. Please find the output and code.
code
-------

    Public Shared Function CreateDoc(ByVal PerChildAList As List(Of String()), ByVal PerCsawAList As List(Of String())) As XElement
        Dim PerChild1List As New List(Of String()) From {New String() {"", "", "", "", "", "Tom", "Jones"},
                                                         New String() {"", "", "", "", "", "Dick", "Turpin"},
                                                         New String() {"", "", "", "", "", "Harry", "Lime"}}
        Dim PerChild2List As New List(Of String()) From {New String() {"", "", "", "", "", "a", "c"},
                                                         New String() {"", "", "", "", "", "b", "d"},
                                                         New String() {"", "", "", "", "", "e", "f"}}
        Dim report = <root>
                         <victim/>
                         <suspect/>
                     </root>
        PerChild1List.ForEach(Sub(l) report.Element("victim").Add(GetVictimPerson(l)))
        PerChild2List.ForEach(Sub(l) report.Element("suspect").Add(GetSuspectPerson(l)))
        Return report
    End Function
    Public Shared Function GetVictimPerson(ByVal L As String())
        Dim v = <victim>
                    <victimPerson>
                        <firstName><%= L(5) %></firstName>
                        <lastName><%= L(6) %></lastName>
                        <phone type="Mobile">555-000-3696</phone>
                        <eMail>test@test.com</eMail>
                        <address type="Business">
                            <address>200 King St</address>
                            <city>Victoria</city>
                            <zipCode>001122</zipCode>
                            <state>VA</state>
                            <nonUsaState></nonUsaState>
                            <country>US</country>
                        </address>
                        <age>20</age>
                        <dateOfBirth>1901-01-01</dateOfBirth>
                    </victimPerson>
                    <screenName>kspesard</screenName>
                    <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
                    <ipCaptureEvent>
                        <ipAddress>192.168.0.1</ipAddress>
                        <eventName>Login</eventName>
                        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                        <dateTime>2001-12-31T12:00:00</dateTime>
                        <timeZone></timeZone>
                    </ipCaptureEvent>
                </victim>

        Return v
    End Function
    Public Shared Function GetSuspectPerson(ByVal L As String())
        Dim v = <suspect>
                    <suspectPerson>
                        <firstName><%= L(5) %></firstName>
                        <lastName><%= L(6) %></lastName>
                        <phone type="Mobile">202-666-7898</phone>

                        <eMail>test@test.com</eMail>
                        <address type="Business">
                            <address>1001 Prince St</address>
                            <city>Alexandria</city>
                            <zipCode>20001</zipCode>
                            <state>VA</state>
                            <nonUsaState></nonUsaState>
                            <country>US</country>
                        </address>
                        <age>100</age>
                        <dateOfBirth>2001-01-01</dateOfBirth>

                    </suspectPerson>
                    <espIdentifier>ABC-000-000</espIdentifier>
                    <screenName>rguede</screenName>
                    <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
                    <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
                    <ipCaptureEvent>
                        <ipAddress>69.174.113.4</ipAddress>
                        <eventName>Login</eventName>
                        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                        <dateTime>2001-12-31T12:00:00</dateTime>
                        <timeZone></timeZone>
                    </ipCaptureEvent>
                </suspect>

        Return v
    End Function


output:
======
<root>
  <victim>
    <victim>
      <victimPerson>
        <firstName>Tom</firstName>
        <lastName>Jones</lastName>
        <phone type="Mobile">555-000-3696</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>200 King St</address>
          <city>Victoria</city>
          <zipCode>001122</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>20</age>
        <dateOfBirth>1901-01-01</dateOfBirth>
      </victimPerson>
      <screenName>kspesard</screenName>
      <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
      <ipCaptureEvent>
        <ipAddress>192.168.0.1</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </victim>
    <victim>
      <victimPerson>
        <firstName>Dick</firstName>
        <lastName>Turpin</lastName>
        <phone type="Mobile">555-000-3696</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>200 King St</address>
          <city>Victoria</city>
          <zipCode>001122</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>20</age>
        <dateOfBirth>1901-01-01</dateOfBirth>
      </victimPerson>
      <screenName>kspesard</screenName>
      <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
      <ipCaptureEvent>
        <ipAddress>192.168.0.1</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </victim>
    <victim>
      <victimPerson>
        <firstName>Harry</firstName>
        <lastName>Lime</lastName>
        <phone type="Mobile">555-000-3696</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>200 King St</address>
          <city>Victoria</city>
          <zipCode>001122</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>20</age>
        <dateOfBirth>1901-01-01</dateOfBirth>
      </victimPerson>
      <screenName>kspesard</screenName>
      <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
      <ipCaptureEvent>
        <ipAddress>192.168.0.1</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </victim>
  </victim>
  <suspect>
    <suspect>
      <suspectPerson>
        <firstName>a</firstName>
        <lastName>c</lastName>
        <phone type="Mobile">202-666-7898</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>1001 Prince St</address>
          <city>Alexandria</city>
          <zipCode>20001</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>100</age>
        <dateOfBirth>2001-01-01</dateOfBirth>
      </suspectPerson>
      <espIdentifier>ABC-000-000</espIdentifier>
      <screenName>rguede</screenName>
      <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
      <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
      <ipCaptureEvent>
        <ipAddress>69.174.113.4</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </suspect>
    <suspect>
      <suspectPerson>
        <firstName>b</firstName>
        <lastName>d</lastName>
        <phone type="Mobile">202-666-7898</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>1001 Prince St</address>
          <city>Alexandria</city>
          <zipCode>20001</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>100</age>
        <dateOfBirth>2001-01-01</dateOfBirth>
      </suspectPerson>
      <espIdentifier>ABC-000-000</espIdentifier>
      <screenName>rguede</screenName>
      <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
      <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
      <ipCaptureEvent>
        <ipAddress>69.174.113.4</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </suspect>
    <suspect>
      <suspectPerson>
        <firstName>e</firstName>
        <lastName>f</lastName>
        <phone type="Mobile">202-666-7898</phone>
        <eMail>test@test.com</eMail>
        <address type="Business">
          <address>1001 Prince St</address>
          <city>Alexandria</city>
          <zipCode>20001</zipCode>
          <state>VA</state>
          <nonUsaState></nonUsaState>
          <country>US</country>
        </address>
        <age>100</age>
        <dateOfBirth>2001-01-01</dateOfBirth>
      </suspectPerson>
      <espIdentifier>ABC-000-000</espIdentifier>
      <screenName>rguede</screenName>
      <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
      <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
      <ipCaptureEvent>
        <ipAddress>69.174.113.4</ipAddress>
        <eventName>Login</eventName>
        <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
        <dateTime>2001-12-31T12:00:00</dateTime>
        <timeZone></timeZone>
      </ipCaptureEvent>
    </suspect>
  </suspect>
</root>

how to avoid victim and suspect tags print at the top...

Kind regards,
Pooja
0
 
MikeTooleCommented:
Pooja,
You need to remove the <victim> element that you've added as the root of the XElement returned by the function:

    Public Shared Function GetVictimPerson(ByVal L As String())
        Dim v = <victim>
                    <victimPerson>
                    <firstName><%= L(5) %></firstName>
...
My example returned a <victimPerson> element to be added to the existing <victim> element:
    Public Shared Function GetVictimPerson(L As String())
        Dim v = <victimPerson>
                    <firstName><%= L(5) %></firstName>
...
This code:
    report.Element("victim").Add(...)
searches the XElement instance report for the first occurance of a child element with the name "victim" and adds the element defined in the paramenter supplied to the Add() method.
 
Mike
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
Thanks. But am looking in this format
<report>
<victim>
                                <victimPerson>
                                                <firstName>Karen</firstName>
                                                <lastName>Spesard</lastName>
                                                <phone type="Mobile">555-000-3696</phone>
                                                <email>kSpesard@email.com</email>
                                                <address type="Business">
                                                                <address>200 King St</address>
                                                                <city>Victoria</city>
                                                                <zipCode>001122</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>20</age>
                                                <dateOfBirth>1901-01-01</dateOfBirth>
                                </victimPerson>
                                <screenName>kspesard</screenName>
                                <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>192.168.0.1</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </victim>
                <victim>
                                <victimPerson>
                                                <firstName>Karen</firstName>
                                                <lastName>Spesard</lastName>
                                                <phone type="Mobile">555-000-3696</phone>
                                                <email>kSpesard@email.com</email>
                                                <address type="Business">
                                                                <address>200 King St</address>
                                                                <city>Victoria</city>
                                                                <zipCode>001122</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>20</age>
                                                <dateOfBirth>1901-01-01</dateOfBirth>
                                </victimPerson>
                                <screenName>kspesard</screenName>
                                <profileUrl type="Login">http://www.blogussion.com/details/site-victim-url-canonicalization/</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>192.168.0.1</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </victim>


<suspect>
                                <suspectPerson>
                                                <firstName>Rudy</firstName>
                                                <lastName>Guede</lastName>
                                                <phone type="Mobile">202-666-7898</phone>
                                                <email verified="true">rguede@email.com</email>
                                                <address type="Business">
                                                                <address>1001 Prince St</address>
                                                                <city>Alexandria</city>
                                                                <zipCode>20001</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>100</age>
                                                <dateOfBirth>2001-01-01</dateOfBirth>
                                </suspectPerson>
                                <espIdentifier>ABC-000-000</espIdentifier>
                                <screenName>rguede</screenName>
                                <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
                                <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>69.174.113.4</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </suspect>
<suspect>
                                <suspectPerson>
                                                <firstName>Rudy</firstName>
                                                <lastName>Guede</lastName>
                                                <phone type="Mobile">202-666-7898</phone>
                                                <email verified="true">rguede@email.com</email>
                                                <address type="Business">
                                                                <address>1001 Prince St</address>
                                                                <city>Alexandria</city>
                                                                <zipCode>20001</zipCode>
                                                                <state>VA</state>
                                                                <nonUsaState></nonUsaState>
                                                                <country>US</country>
                                                </address>
                                                <age>100</age>
                                                <dateOfBirth>2001-01-01</dateOfBirth>
                                </suspectPerson>
                                <espIdentifier>ABC-000-000</espIdentifier>
                                <screenName>rguede</screenName>
                                <profileUrl type="Login">http://www.atheists.org/news/Terror_Suspect%27s_Web_Posts_Reveal_Desire_For_%27Great_Jihad,%27_Shame_About_Sex</profileUrl>
                                <profileUrl type="Registration">http://games.slashdot.org/story/11/01/13/1839215/FBI-Seeks-Suspects-Web-Game-Records?from=rss</profileUrl>
                                <ipCaptureEvent>
                                                <ipAddress>69.174.113.4</ipAddress>
                                                <eventName>Login</eventName>
                                                <cookieInfo>SHDJFBNDN89dhBD6</cookieInfo>
                                                <dateTime>2001-12-31T12:00:00</dateTime>
                                                <timeZone></timeZone>
                                </ipCaptureEvent>
                </suspect>


</report>


Kind regards,
Pooja
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
Your code works . Please give me some time to sneak in little bit...Thanks Mike..

Kind regards,
Pooja
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
How to add xdeclaration to this xdoc. What i did is
 Dim xdoc = New XDocument(New XDeclaration("1.0", "utf-8", "yes"))
        Dim xelem As XElement
        xelem = CreateDoc(PersistIntake.PersistChildArrayList, PersistIntake.PersistCsawArrayList)
        xdoc.Add(xelem)

I could not see this declaration at the top.Also once i able to generate the xml (actual xml with xml tag ) , i need to parse each and every element and assign to collection..

Kind regards,
Pooja
0
 
MikeTooleCommented:
You may have missed the declaration, it's a separate property on the XDocument object and doesn't show in the element tree, but it will be added to the file when the document is saved.
You can also create the document directly using an xml literal. The following changes to my original code creates ,populates and then saves an xml document.
The declaration of XDoc replaces the previous use of the variable 'report':

...
        Dim XDoc = <?xml version="1.0" encoding="utf-8" standalone="yes"?>
                   <report>
                       <victim/>
                       <suspect/>
                   </report>
        Dim reportElement = XDoc.Element("report")
        PerChildAList.ForEach(Sub(l) reportElement.Element("victim").Add(GetVictimPerson(l)))
        XDoc.Save(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "MyFile.xml"))
...
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
Ok. Instead of saving a file , is there a way to send in stream. Because i have to send this xmldocument to webservice which accepts stream.

Also just curious to know i tried to save , it is not saving. Never mind if this takes additional time.

Kind regards,
Pooja
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
What mistake i am doing here. I tried to write in stream
 Dim PerChild1List As New List(Of String()) From {New String() {"", "", "", "", "", "Tom", "Jones"},
                                                         New String() {"", "", "", "", "", "Dick", "Turpin"},
                                                         New String() {"", "", "", "", "", "Harry", "Lime"}}
        Dim PerChild2List As New List(Of String()) From {New String() {"", "", "", "", "", "a", "c"},
                                                         New String() {"", "", "", "", "", "b", "d"},
                                                         New String() {"", "", "", "", "", "e", "f"}}

Dim ms As New MemoryStream()
        Dim xws As New XmlWriterSettings()
        xws.OmitXmlDeclaration = True
        xws.Indent = True
        Using xw As XmlWriter = XmlWriter.Create(ms, xws)
            Dim XDoc = <?xml version="1.0" encoding="utf-8" standalone="yes"?>
                       <report>
                           <victim/>
                           <suspect/>
                       </report>
            Dim reportElement = XDoc.Element("report")
            PerChild1List.ForEach(Sub(l) reportElement.Element("victim").Add(GetVictimPerson(l)))
            PerChild2List.ForEach(Sub(l) reportElement.Element("victim").Add(GetVictimPerson(l)))
            XDoc.WriteTo(xw)
            Dim bt As Byte() = ms.ToArray
            bt = ms.ToArray
            ms.Close()
            MessageBox.Show(System.Text.Encoding.UTF8.GetString(bt, 0, bt.Length))
            Return XDoc
        End Using

Kind regards,
Pooja

0
 
MikeTooleCommented:
I believe that this will create a stream, but I've not tested it:

        Dim ms As New System.IO.MemoryStream
        Dim xw = XmlWriter.Create(ms)
        XDoc.WriteTo(xw)

There are options to configure the XmlWriter to control whitespace, etc.
0
 
poojasureshkumarAuthor Commented:
Hi Mike ,
It is not working.Do you have any link to follow.

Kind regards,
Pooja
0
 
poojasureshkumarAuthor Commented:
Hi Mike,
I got it working. Let me have some more time.Thanks Mike.

Kind regards,
Pooja
0
 
poojasureshkumarAuthor Commented:
Excellent. Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now