Solved

Write XML to file using XDocument

Posted on 2014-10-25
4
451 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

23 Experts available now in Live!

Get 1:1 Help Now