Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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