简体   繁体   English

将.xml文件读入XmlDocument

[英]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: 我可以像这样将其读入XmlDocument

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. Windows Phone 8.1不支持XmlDocument.Load ,因此我不能仅输入文件名。 System.IO.File.ReadAllText(fileName); is also unacceptable to Windows Phone 8.1. Windows Phone 8.1也无法接受。 XDocument did not seem to have a friendly method. XDocument似乎没有友好的方法。

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? 如何将.xml文件读取为string以便可以将其插入Windows Phone 8.1应用程序的XmlDocument中?

XDocument.Load can load from the file. XDocument.Load可以从文件加载。 It is supported in Windows Phone 8.1, according to MSDN: 根据MSDN,Windows Phone 8.1支持此功能:

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1 支持于:Windows Phone 8.1,Windows Phone 8,Silverlight 8.1

XDocument.Parse loads from a string, containing XML. XDocument.Parse从包含XML的字符串加载。

Regarding conversion from XDocument to XmlDocument, you can use @Muhammad's answer . 关于从XDocument到XmlDocument的转换,可以使用@Muhammad的answer If you decide to implement it, consider a potential performance issue with large XML files (read my comment underneath it). 如果您决定实施它,请考虑使用大型XML文件的潜在性能问题(请阅读其下面的评论)。

Here is how you can do it in xml document. 这是您可以在xml文档中执行的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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