Solved

Write XML to file using XDocument

Posted on 2014-10-25
4
456 Views
Last Modified: 2014-11-12
Hi all,
I have this code from Microsoft to create an xml file; but I can't find a way to write it to a file to my Windows Phone 8.1 project.

Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True

Using xw = XmlWriter.Create(sb, xws)
    Dim doc As XDocument = New XDocument(<Child><GrandChild>some content</GrandChild></Child>)
    doc.WriteTo(xw)
End Using

Console.WriteLine(sb.ToString())

Open in new window

0
Comment
Question by:Thunder724
  • 3
4 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 40404279
Pass a Stream to the XmlWriter.Create method instead of a StringBuilder.
Not sure FileStream is available on Windows Phone. I guess you can use an IsolatedStorageFileStream
0
 
LVL 4

Author Comment

by:Thunder724
ID: 40414180
Nope that didn't work either.
I did manage to fix it though using a different technique....

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

        Dim xDoc As New XDocument(
            New XDeclaration("1.0", "utf-8", "yes") _
            , New XElement("RUSAFE" _
                , New XElement("INFOTBL",
                    New XElement("FILE_NAME", New XText(txtSAFE_NAME.Text.ToString())))))

st = Await sf.CreateFileAsync(XMLDOC$, CreationCollisionOption.OpenIfExists)
        Dim ws = TryCast(Await st.OpenStreamForWriteAsync(), Stream)
        xDoc.Save(ws)
 _

Open in new window


This does work; but the only issue i'm having wih it is that sometimes the data Is not saved.  I'm wondering if the Await function is causing a problem or perhaps I have to close the file. Strange, it works sometimes and not other..
0
 
LVL 4

Accepted Solution

by:
Thunder724 earned 0 total points
ID: 40414200
Think I fixed it.
ws.dispose on the end.

I need to test it some more but it seems to be consistant at the moment.
0
 
LVL 4

Author Closing Comment

by:Thunder724
ID: 40421163
I figured out a completely different way of solving the problem as all other solutions did not work.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 …

861 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