Solved

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

Posted on 2014-10-17
5
654 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now