|
-
Sep 23rd, 2011, 03:55 PM
#1
Thread Starter
Addicted Member
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
-
Sep 23rd, 2011, 05:00 PM
#2
Re: XML structure to string
try this:
vb Code:
Public Class Form1
''' <summary>
''' XmlTextWriter
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks>writes an xml fragment
''' uses 4 textboxes for inputting attribute values</remarks>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IO.File.Exists("test.txt") Then
IO.File.Delete("test.txt")
End If
Dim fs As New IO.FileStream("test.txt", IO.FileMode.CreateNew)
Using writer As New Xml.XmlTextWriter(fs, System.Text.Encoding.UTF8)
writer.WriteStartElement("file")
writer.WriteStartElement("node")
writer.WriteAttributeString("name", "", TextBox1.Text)
writer.WriteAttributeString("value", "", TextBox2.Text)
writer.WriteEndElement()
writer.WriteStartElement("anothernode")
writer.WriteAttributeString("name", "", TextBox3.Text)
writer.WriteAttributeString("value", "", TextBox4.Text)
writer.WriteEndElement()
writer.WriteEndElement()
End Using
'check file
Process.Start("test.txt")
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 23rd, 2011, 10:49 PM
#3
Re: XML structure to string
Another alternate using IO.File.WriteAllText and LINQ
Code:
Dim Content = _
<People>
<Person>
<FirstName><%= 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
-
Sep 26th, 2011, 09:06 AM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|