Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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