C# XML ReadLine到txt文件
我在使用XMLReader将XML URL节点中的数据保存到文本文件时遇到问题。你能帮帮我吗?我不知道怎么做 代码如下:C# XML ReadLine到txt文件,c#,C#,我在使用XMLReader将XML URL节点中的数据保存到文本文件时遇到问题。你能帮帮我吗?我不知道怎么做 代码如下: namespace XMLdemo2 { class Program { static void Main(string[] args) { // Start with XmlReader object String URLString = "https://www.short
namespace XMLdemo2
{
class Program
{
static void Main(string[] args)
{
// Start with XmlReader object
String URLString = "https://www.shortcut.lv/xmls/tiesraide/ltv1.xml";
XmlTextReader reader = new XmlTextReader(URLString);
{
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name.ToString())
{
case "auth_token":
Console.WriteLine("Tokens IR : " + reader.ReadString());
break;
}
//Console.WriteLine("");
}
}
Console.ReadKey();
}
}
}
}
你可以试试这样简单的东西(如果你只想读一行) 如果是多行
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList itemNodes = xmlDoc.SelectNodes("//auth_token");
foreach(XmlNode itemNode in itemNodes)
{
if((itemNode != null)
Console.WriteLine(itemNode.InnerText);
}
你的问题不清楚你想输出什么。您正试图从XMLReader中写入什么?您也可以重命名该文件…如果没有太多要写入的文本,为了简单起见,您可以使用。看起来您想要的只是从该XML文件中获取auth_标记的值?为什么不直接使用XPath读取该值,而不是将其写入文本文件并从该文件中读回?
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList itemNodes = xmlDoc.SelectNodes("//auth_token");
foreach(XmlNode itemNode in itemNodes)
{
if((itemNode != null)
Console.WriteLine(itemNode.InnerText);
}