Solved

WINDOWS PHONE 8.1 DEVELOPMENT (Create an XML Document) No file

Posted on 2014-10-17
5
698 Views
Last Modified: 2014-11-12
Hi all,
I have been trying to figure out how to create an XML document.  I've found many code examples; but don't see where the file is specified in any of them.  Below is the code I have so far and have a file variable (string) but can't seem to fit it in anyplace.

Dim XMLDOC$ = txtSAFE_NAME.Text & ".xml"

        ' Create XmlWriterSettings.
        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True
        Dim sw As New StringWriter()


        ' Create XmlWriter.
        Using writer As XmlWriter = XmlWriter.Create(sw, settings)
            writer.WriteStartDocument()
            writer.WriteStartElement("RUSAFE") ' Root.

            writer.WriteStartElement("INFOTBL")
            writer.WriteElementString("FILE_NAME", txtSAFE_NAME.Text.ToString)

            writer.WriteEndElement()

            ' End document.
            writer.WriteEndElement()
            writer.WriteEndDocument()

            writer.Flush()

            Dim output As String = sw.ToString()

        End Using

Open in new window

0
Comment
Question by:Thunder724
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40388636
You could try something like this, which uses the LINQ-to-XML XDocument:

Imports System.Xml.Linq

Public Class Test

	Public Shared Sub WriteXmlDocument(fileName As String)
		Dim document = New XDocument()

		Dim rootElement = 
                     New XElement("RUSAFE",
                          New XElement("INFOTBL", 
                             New XAttribute("FILE_NAME", txtSAFE_NAME.Text.ToString())))

		document.Add(rootElement)

		document.Save(fileName)
	End Sub
End Class

Open in new window

0
 
LVL 4

Author Comment

by:Thunder724
ID: 40388718
Exact same problem.  When getting to the "filename" an error is presented that I have to use xmlwriter, stream or textwriter; but all have the same error. "Value of type string can not be converted"
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 40389377
Can you attach the full stack trace for the exception?
0
 
LVL 4

Author Comment

by:Thunder724
ID: 40392740
Ok, I got it to work with the help of some code I found and Bob's imput.

Dim XMLDOC$ = txtSAFE_NAME.Text & ".xml"
        Dim sf As Windows.Storage.StorageFolder = RSCatalog_Loc
        Dim st As StorageFile = Await sf.CreateFileAsync(XMLDOC$)

        Dim rootElement =
                     New XElement("SAFE",
                          New XElement("INFOTBL",
                             New XAttribute("FILE_NAME", txtSAFE_NAME.Text.ToString())),
                          New XElement("MAINTBL",
                             New XAttribute("SID", "124"),
                             New XAttribute("sITEM_NAME", "DOCUMENT 1"),
                             New XAttribute("SNOTES", "THIS IS A NOTE")))

        Await writeXml(rootElement, st)

Open in new window


Private Async Function writeXml(Of T)(Data As T, file As StorageFile) As Task
        Try
            Dim sw As New StringWriter()
            Dim xmlser As New XmlSerializer(GetType(T))
            xmlser.Serialize(sw, Data)

            Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.ReadWrite)
                Using outputStream As IOutputStream = fileStream.GetOutputStreamAt(0)
                    Using dataWriter As New DataWriter(outputStream)
                        dataWriter.WriteString(sw.ToString())
                        Await dataWriter.StoreAsync()
                        dataWriter.DetachStream()
                    End Using

                    Await outputStream.FlushAsync()
                End Using


            End Using
        Catch e As Exception

            Throw New NotImplementedException(e.Message.ToString())
        End Try

    End Function

Open in new window

0
 
LVL 4

Author Comment

by:Thunder724
ID: 40392745
The function I found was at the following location from "Shivam cv"
http://www.developerfusion.com/tools/convert/csharp-to-vb/?batchId=dc03592b-03b7-4a5b-863e-8736f78856d5
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question