Solved

Write XML to file using XDocument

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Vb.net threads keep increasing 2 49
Why use this lambda? 12 60
VB.Net  Exporting Excel Woorkbook to PDF 7 58
c#, case, if 4 17
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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