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 に答える