C# XmlTextReader仅在控制台应用程序中工作?
我在一个控制台应用程序中有一个rss阅读器,它可以正常工作:C# XmlTextReader仅在控制台应用程序中工作?,c#,xml-parsing,C#,Xml Parsing,我在一个控制台应用程序中有一个rss阅读器,它可以正常工作: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { XmlTextReader reader = new XmlTextReader("http://www.di.se/rss"); string myXmlString = "
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader("http://www.di.se/rss");
string myXmlString = "";
// Skip non-significant whitespace
reader.WhitespaceHandling = WhitespaceHandling.Significant;
// Read nodes one at a time
while (reader.Read())
{
// Print out info on node
//Console.WriteLine(reader.ReadInnerXml().ToString());
myXmlString += myXmlString + reader.ReadInnerXml().ToString();
}
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + myXmlString + "</root>");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i = 0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
Console.ReadLine();
}
}
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
XmlTextReader=新的XmlTextReader(“http://www.di.se/rss");
字符串myXmlString=“”;
//跳过不重要的空白
reader.WhitespaceHandling=WhitespaceHandling.signific;
//一次读取一个节点
while(reader.Read())
{
//打印节点上的信息
//Console.WriteLine(reader.ReadInnerXml().ToString());
myXmlString+=myXmlString+reader.ReadInnerXml().ToString();
}
//创建XmlDocument。
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“+myXmlString+”);
//显示所有书名。
XmlNodeList elemList=doc.GetElementsByTagName(“标题”);
for(int i=0;i
然而,当我尝试将它移到我的Windows通用应用程序时,它说XmlTextReader命名空间不存在。这是否仅适用于控制台应用程序?如何将其转换为我的通用应用程序?我在我的通用应用程序中声明的名称空间与我的控制台项目中声明的名称空间完全相同(然后是一些),因此我尝试在mainpage.xaml.cs文件中运行它
我尝试使用
XmlReader=newxmlreader(“http://www.di.se/rss");代码>,但是没有用。通用应用程序应该可以很好地支持XmlReader。确认项目中引用了System.Xml程序集。通用应用程序应该可以很好地支持XmlReader。确认项目中引用了System.Xml程序集。在XmlReader
上调用Create()
以获取可用流,如以下示例所示:
using (var reader = XmlReader.Create(path))
{
this.XmlDocument.Load(reader);
}
在XmlReader
上调用Create()
,以获取可用流,如下例所示:
using (var reader = XmlReader.Create(path))
{
this.XmlDocument.Load(reader);
}
确保使用XMLReader所需的库构建项目,可能需要在新应用中添加库,但通用应用不需要添加库。确保使用XMLReader所需的库构建项目,可能需要在新应用中添加库,虽然通用应用程序不需要这样做。我可能已经重复了这个问题。在公共控制台应用程序(使用Visual Studio中的默认控制台应用程序模板创建)中,引用System.Xml程序集。但在UWP中,它并没有在默认模板中被引用,并且assembly System.Xml不能像在控制台应用程序中那样简单地从UWP引用
要解决此问题,应遵循以下步骤:
将NuGet包添加到UWP应用程序中,以便使用类似System.XML.ReaderWriter的XML
使用XmlReader有点不同,因为您不能像在控制台应用程序中那样简单地访问URI。您应该创建HttpClient,获取一个流并将其传递给XmlReader。
在这里,我为Main.xaml.cs提供了一个示例代码,该代码随默认UWP应用程序模板一起提供:
using System.Net.Http;
using System.Xml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var client = new HttpClient();
var stream = client.GetStreamAsync("http://www.di.se/rss").Result;
XmlReader reader = XmlReader.Create(stream);
string myXmlString = "";
while (reader.Read())
{
myXmlString += myXmlString + reader.ReadInnerXml().ToString();
}
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + myXmlString + "</root>");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i = 0; i < elemList.Count; i++)
{
var xml = elemList[i].InnerXml;
// Do your work with xml
}
}
}
}
使用System.Net.Http;
使用System.Xml;
使用Windows.UI.Xaml.Controls;
名称空间App1
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
var client=新的HttpClient();
var stream=client.GetStreamAsync(“http://www.di.se/rss三、结果;
XmlReader=XmlReader.Create(流);
字符串myXmlString=“”;
while(reader.Read())
{
myXmlString+=myXmlString+reader.ReadInnerXml().ToString();
}
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“+myXmlString+”);
//显示所有书名。
XmlNodeList elemList=doc.GetElementsByTagName(“标题”);
for(int i=0;i
我可能已经复制了这个问题。在公共控制台应用程序(使用Visual Studio中的默认控制台应用程序模板创建)中,引用System.Xml程序集。但在UWP中,它并没有在默认模板中被引用,并且assembly System.Xml不能像在控制台应用程序中那样简单地从UWP引用
要解决此问题,应遵循以下步骤:
将NuGet包添加到UWP应用程序中,以便使用类似System.XML.ReaderWriter的XML
使用XmlReader有点不同,因为您不能像在控制台应用程序中那样简单地访问URI。您应该创建HttpClient,获取一个流并将其传递给XmlReader。
在这里,我为Main.xaml.cs提供了一个示例代码,该代码随默认UWP应用程序模板一起提供:
using System.Net.Http;
using System.Xml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var client = new HttpClient();
var stream = client.GetStreamAsync("http://www.di.se/rss").Result;
XmlReader reader = XmlReader.Create(stream);
string myXmlString = "";
while (reader.Read())
{
myXmlString += myXmlString + reader.ReadInnerXml().ToString();
}
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + myXmlString + "</root>");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i = 0; i < elemList.Count; i++)
{
var xml = elemList[i].InnerXml;
// Do your work with xml
}
}
}
}
使用System.Net.Http;
使用System.Xml;
使用Windows.UI.Xaml.Controls;
名称空间App1
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
var client=新的HttpClient();
var stream=client.GetStreamAsync(“http://www.di.se/rss三、结果;
XmlReader=XmlReader.Create(流);
字符串myXmlString=“”;
while(reader.Read())
{
myXmlString+=myXmlString+reader.ReadInnerXml().ToString();
}
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“+myXmlString+”);
//显示所有书名。
Xm