Results 1 to 4 of 4

Thread: XML structure to string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    234

    XML structure to string

    I need a little bit of help writing an XML-formatted string so that I may later encrypt it.

    If I create an XMLDocument, it will insert its formatting tag <?xml version="1.0" encoding="Windows-1252"?> and I need to avoid this.

    I'm not very familiar with streams, or how to output an XMLWriter to one.

    How can I get this structure "<file><node name="" value=""><anothernode name="" value=""></file>"

    Thanks in advance for any tips, links or pseudocode. anything helps.
    Last edited by unxzst; Sep 23rd, 2011 at 03:55 PM. Reason: ninja edit

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: XML structure to string

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     ''' <summary>
    4.     ''' XmlTextWriter
    5.     ''' </summary>
    6.     ''' <param name="sender"></param>
    7.     ''' <param name="e"></param>
    8.     ''' <remarks>writes an xml fragment
    9.     ''' uses 4 textboxes for inputting attribute values</remarks>
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         If IO.File.Exists("test.txt") Then
    12.             IO.File.Delete("test.txt")
    13.         End If
    14.         Dim fs As New IO.FileStream("test.txt", IO.FileMode.CreateNew)
    15.         Using writer As New Xml.XmlTextWriter(fs, System.Text.Encoding.UTF8)
    16.             writer.WriteStartElement("file")
    17.  
    18.             writer.WriteStartElement("node")
    19.             writer.WriteAttributeString("name", "", TextBox1.Text)
    20.             writer.WriteAttributeString("value", "", TextBox2.Text)
    21.             writer.WriteEndElement()
    22.  
    23.             writer.WriteStartElement("anothernode")
    24.             writer.WriteAttributeString("name", "", TextBox3.Text)
    25.             writer.WriteAttributeString("value", "", TextBox4.Text)
    26.             writer.WriteEndElement()
    27.  
    28.             writer.WriteEndElement()
    29.         End Using
    30.         'check file
    31.         Process.Start("test.txt")
    32.     End Sub
    33.  
    34. End Class

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: XML structure to string

    Another alternate using IO.File.WriteAllText and LINQ
    Code:
    Dim Content = _
    <People>
        <Person>
            <FirstName><&#37;= txtFirstName.Text %></FirstName>
            <LastName><%= txtLastName.Text %></LastName>
        </Person>
        <Person>
            <FirstName>Kevin</FirstName>
            <LastName>Gallagher</LastName>
        </Person>
        <Person>
            <FirstName>Mary</FirstName>
            <LastName>Gallagher</LastName>
        </Person>
        <Person>
            <FirstName>Terry</FirstName>
            <LastName>Adams</LastName>
        </Person>
    </People>
    
    IO.File.WriteAllText("MyFile.xml", Content.ToString)
    Dim Document As New XDocument
    Document = XDocument.Load("MyFile.xml")
    Dim People = (From P In Document...<Person> _
                  Select FirstName = P.<FirstName>.Value, LastName = P.<LastName>.Value _
                  Order By LastName).ToList
    
    For Each person In People
        Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
    Next
    Or

    Code:
    Dim Content = <People></People>
    
    Content.Add( _
    <Person>
        <FirstName><%= txtFirstName.Text %></FirstName>
        <LastName><%= txtLastName.Text %></LastName>
    </Person>)
    
    IO.File.WriteAllText("MyFile.xml", Content.ToString)
    
    
    Dim Document As New XDocument
    Document = XDocument.Load("MyFile.xml")
    Dim People = (From P In Document...<Person> _
                  Select FirstName = P.<FirstName>.Value, LastName = P.<LastName>.Value _
                  Order By LastName).ToList
    
    For Each person In People
        Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
    Next

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    234

    Re: XML structure to string

    thanks for your help. this is what I was after:
    Code:
            Dim memorystream As New MemoryStream()
            Dim xmlwriter As New XmlTextWriter(memorystream, System.Text.Encoding.UTF8)
    
            xmlwriter.WriteStartElement("option")
            xmlwriter.WriteString(TextBox1.Text)
            xmlwriter.WriteEndElement()
    
            xmlwriter.Flush()
    
            Dim stream_reader As New StreamReader(memorystream)
    
            memorystream.Seek(0, SeekOrigin.Begin)
    
            Console.WriteLine(stream_reader.ReadToEnd())

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width