1

I have a live tile template such as:

<tile>
  <visual version="2">
    <binding template="TileSquare150x150Text02" fallback="TileSquareText02">
      <text id="1">Text Field 1 (larger text)</text>
      <text id="2">Text Field 2</text>
    </binding>  
  </visual>
</tile>

I can read it into an XmlDocument like so:

StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?><tile>");
            sb.Append("<visual version=\"2\"><binding template=\"TileSquare150x150Text04\" fallback=\"TileSquareText04\"><text id=\"1\">Text Field 1</text></binding></visual></tile>");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(sb.ToString());

But I would really like to read it directly from a file, as this will quickly become extremely messy.

XmlDocument.Load is not supported for Windows Phone 8.1, so I cannot just pump in the filename. System.IO.File.ReadAllText(fileName); is also unacceptable to Windows Phone 8.1. XDocument did not seem to have a friendly method.

What can I do to read the .xml file to a string so I can plug it into XmlDocument for a Windows Phone 8.1 app?

Evorlor
  • 7,263
  • 17
  • 70
  • 141

2 Answers2

3

XDocument.Load can load from the file. It is supported in Windows Phone 8.1, according to MSDN:

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

XDocument.Parse loads from a string, containing XML.

Regarding conversion from XDocument to XmlDocument, you can use @Muhammad's answer. If you decide to implement it, consider a potential performance issue with large XML files (read my comment underneath it).

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Well I'll be! I was initializing the XDocument then trying to load the instantiation. Thanks! XDocument xDoc = XDocument.Load("filename"); – Evorlor Jul 26 '14 at 23:20
  • unforunately I just found out that I need an XmlDocument. wonder if there is a way to convert... – Evorlor Jul 26 '14 at 23:21
  • @Evorlor: Why do you need to be using XmlDocument? XDocument is very user-friendly, it usually has less lines of code. Converting should be easy, if it's not a lot of code. – Victor Zakharov Jul 26 '14 at 23:23
  • TileNotification requires an XmlDocument in its constructor, not an XDocument. But I can convert it to a string and then make an XmlDocument from there. Success! ugly success....but success! Thanks :) – Evorlor Jul 26 '14 at 23:25
  • @Evorlor: Check this out: http://stackoverflow.com/questions/1508572/converting-xdocument-to-xmldocument-and-vice-versa – Victor Zakharov Jul 26 '14 at 23:26
  • i have read it already. thats a lot of unnecessary code. see muhammed's answer above – Evorlor Jul 26 '14 at 23:28
  • @Evorlor: It may not be as efficient, check another answer from the same thread (and comments underneath): http://stackoverflow.com/a/9376479/897326 More code is not always bad. – Victor Zakharov Jul 26 '14 at 23:28
  • I read that as well, and even tried it. Won't work with Windows Phone 8.1. but i know what you mean. this operation will only be run once when the app closes though, so im not too concerned about inefficiency. – Evorlor Jul 26 '14 at 23:29
  • I don't know where to put the checkmark. Muhammad's answer is the full answer, but your answer is what led there! – Evorlor Jul 26 '14 at 23:34
  • @Evorlor: Flip a coin? :) – Victor Zakharov Jul 26 '14 at 23:35
  • perhaps you could link to his answer in yours? :-) – Evorlor Jul 26 '14 at 23:36
1

Here is how you can do it in xml document.

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(XDocument.Load("Assets/test.xml").ToString());
Muhammad Saifullah
  • 4,292
  • 1
  • 29
  • 59
  • 1
    While this may look like a nice solution, the following thread suggests it may have performance issues due to string conversion (back and forth). It should, however, work well for small XML files, if that's what you are after. For more information, check a comment under this answer. http://stackoverflow.com/a/9376479/897326 – Victor Zakharov Jul 26 '14 at 23:31
  • Neolisk is right. But anyone who is using this for live tiles, you shouldn't be running this code more than once per session anyways. – Evorlor Jul 26 '14 at 23:32