Read .xml File into XmlDocument

早过忘川 提交于 2019-12-13 02:37:43

问题


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?


回答1:


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

  • XDocument.Load Method (String)

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




回答2:


Here is how you can do it in xml document.

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


来源:https://stackoverflow.com/questions/24976348/read-xml-file-into-xmldocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!