简体   繁体   中英

Read .xml File into XmlDocument

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?

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).

Here is how you can do it in xml document.

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(XDocument.Load("Assets/test.xml").ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM