将.xml文件读入XmlDocument [英] Read .xml File into XmlDocument

查看:128
本文介绍了将.xml文件读入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>

我可以像这样将其读入XmlDocument:

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,因此我不能仅输入文件名. System.IO.File.ReadAllText(fileName);也不是Windows Phone 8.1可接受的. XDocument似乎没有友好的方法.

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.

如何将.xml文件读取为string,以便可以将其插入Windows Phone 8.1应用程序的XmlDocument中?

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可以从文件加载.根据MSDN,Windows Phone 8.1支持该功能:

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

受支持:Windows Phone 8.1,Windows Phone 8,Silverlight 8.1

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

XDocument.Parse从包含XML的字符串中加载.

XDocument.Parse loads from a string, containing XML.

关于从XDocument到XmlDocument的转换,您可以使用 @Muhammad的答案.如果您决定实施它,请考虑使用大型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).

这篇关于将.xml文件读入XmlDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆