Solved

Write XML to file using XDocument

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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