0

我正在尝试读取由 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");
        }
    }
4

2 回答 2

1

不应该那样使用 SelectSingleNode 。你需要一个 XPath 查询,而不是一个顶级节点。在此处查找有关 XPath 的信息

你需要尝试这样的事情:

EMAIL = xDoc.CreateNavigator().SelectSingleNode("/PROGRAM/EMAIL").Value;
于 2012-07-19T22:58:06.003 回答
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
    }
}
于 2012-07-19T23:24:52.047 回答