Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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