我正在尝试阅读由ASP脚本创建的动态xml,但不幸的是我无法做到。看来我的C#代码无法理解ASP脚本。
我很乐意听到一些建议。
您可以在此处查看ASP脚本:http://www.ad-net.co.il/test.asp
<%
Response.Buffer = True
Response.ContentType = "text/xml"
body = ""
body = body & "<?xml version=""1.0"" encoding=""utf-8""?>"
body = body & "<PROGRAM>"
body = body & "<EMAIL>email@gmail.com</EMAIL>"
body = body & "<USERNAME>username@gmail.com</USERNAME>"
body = body & "<PASSWORD>password</PASSWORD>"
body = body & "</PROGRAM>"
Response.Write(body)
%>
C#代码是:
private static void LoadXmlFromServerToProgram()
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://www.ad-net.co.il/test.asp");
EMAIL = xDoc.DocumentElement.SelectSingleNode("EMAIL").InnerText;
USERNAME = xDoc.DocumentElement.SelectSingleNode("USERNAME").InnerText;
PASSWORD = xDoc.DocumentElement.SelectSingleNode("PASSWORD").InnerText;
}
catch
{
MessageBox.Show("Can't Read From XML");
}
}
答案 0 :(得分:1)
你不应该以这种方式使用SelectSingleNode ..你需要一个XPath查询,而不是一个顶级节点。 Look here for information on XPath
你需要尝试这样的事情:
EMAIL = xDoc.CreateNavigator().SelectSingleNode("/PROGRAM/EMAIL").Value;
答案 1 :(得分:0)
试试这个:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace TestApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form1.LoadXmlFromServerToProgram();
MessageBox.Show(string.Concat("Email: ", Email, "\r\n", "UserName: ", UserName, "\r\n", "Password: ", Password));
}
public static string Email { get; set; }
public static string UserName { get; set; }
public static string Password { get; set; }
private static void LoadXmlFromServerToProgram()
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://www.ad-net.co.il/test.asp");
Email = xDoc.DocumentElement.SelectSingleNode("EMAIL").InnerText;
UserName = xDoc.DocumentElement.SelectSingleNode("USERNAME").InnerText;
Password = xDoc.DocumentElement.SelectSingleNode("PASSWORD").InnerText;
}
catch
{
MessageBox.Show("Can't Read From XML");
}
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
}
}